Attempts to convert the args/kwargs into the format defined in self.format and self.timeformat
(self, *args, **kwargs)
| 156 | logger.log(level, msg, *args, **kwargs) |
| 157 | |
| 158 | def convert(self, *args, **kwargs): |
| 159 | """ |
| 160 | Attempts to convert the args/kwargs into the format defined in |
| 161 | self.format and self.timeformat |
| 162 | """ |
| 163 | # Have the keyword arguments default to empty strings, in the event |
| 164 | # of missing keys for string formatting |
| 165 | outdict = defaultdict(str, **kwargs) |
| 166 | outformat = self.format |
| 167 | extras = [] |
| 168 | |
| 169 | # Convert raw timestamps into a datetime object |
| 170 | if 'ts' in outdict: |
| 171 | try: |
| 172 | outdict['ts'] = datetime.fromtimestamp(float(outdict['ts'])) |
| 173 | outdict['ts'] = outdict['ts'].strftime(self.timeformat) |
| 174 | except TypeError: |
| 175 | pass |
| 176 | except KeyError: |
| 177 | pass |
| 178 | except ValueError: |
| 179 | pass |
| 180 | |
| 181 | if "starttime" in outdict and isinstance(outdict["starttime"], datetime): |
| 182 | outdict['starttime'] = outdict['starttime'].strftime(self.timeformat) |
| 183 | if "endtime" in outdict and isinstance(outdict["endtime"], datetime): |
| 184 | outdict['endtime'] = outdict['endtime'].strftime(self.timeformat) |
| 185 | if 'dt' in outdict and isinstance(outdict["dt"], datetime): |
| 186 | outdict['dt'] = outdict['dt'].strftime(self.timeformat) |
| 187 | |
| 188 | # Create directional arrows |
| 189 | if 'dir_arrow' not in outdict: |
| 190 | if outdict.get('direction') == 'cs': |
| 191 | outdict['dir_arrow'] = '->' |
| 192 | elif outdict.get('direction') == 'sc': |
| 193 | outdict['dir_arrow'] = '<-' |
| 194 | else: |
| 195 | outdict['dir_arrow'] = '--' |
| 196 | |
| 197 | # Convert Nones into empty strings. |
| 198 | # If --extra flag used, generate string representing otherwise hidden |
| 199 | # fields. |
| 200 | for key, val in sorted(outdict.items()): |
| 201 | if val is None: |
| 202 | val = '' |
| 203 | outdict[key] = val |
| 204 | if self.extra: |
| 205 | if key not in self.format_fields: |
| 206 | extras.append("%s=%s" % (key, val)) |
| 207 | |
| 208 | # Dump the args into a 'data' field |
| 209 | outdict['data'] = self.delimiter.join(map(str, args)) |
| 210 | |
| 211 | # Create an optional 'extra' field |
| 212 | if self.extra: |
| 213 | if 'extra' not in self.format_fields: |
| 214 | outformat = outformat[:-1] + " [ %(extra)s ]\n" |
| 215 | outdict['extra'] = ', '.join(extras) |