| 245 | DEFAULT_LOG_LEVEL = 6 # info |
| 246 | |
| 247 | def format(self, record): |
| 248 | attributes = self._get_extra_attributes(record=record) |
| 249 | attributes = self._format_extra_attributes(attributes=attributes) |
| 250 | |
| 251 | msg = record.msg |
| 252 | exc_info = record.exc_info |
| 253 | time_now_float = record.created |
| 254 | time_now_sec = int(time_now_float) |
| 255 | level = self.PYTHON_TO_GELF_LEVEL_MAP.get( |
| 256 | record.levelno, self.DEFAULT_LOG_LEVEL |
| 257 | ) |
| 258 | |
| 259 | common_attributes = self._get_common_extra_attributes(record=record) |
| 260 | full_msg = super(GelfLogFormatter, self).format(record) |
| 261 | |
| 262 | data = { |
| 263 | "version": GELF_SPEC_VERSION, |
| 264 | "host": HOSTNAME, |
| 265 | "short_message": msg, |
| 266 | "full_message": full_msg, |
| 267 | "timestamp": time_now_sec, |
| 268 | "timestamp_f": time_now_float, |
| 269 | "level": level, |
| 270 | } |
| 271 | |
| 272 | if exc_info: |
| 273 | # Include exception information |
| 274 | exc_type, exc_value, exc_tb = exc_info |
| 275 | tb_str = "".join(traceback.format_tb(exc_tb)) |
| 276 | data["_exception"] = six.text_type(exc_value) |
| 277 | data["_traceback"] = tb_str |
| 278 | |
| 279 | # Include common Python log record attributes |
| 280 | data["_python"] = common_attributes |
| 281 | |
| 282 | # Include user extra attributes |
| 283 | data.update(attributes) |
| 284 | |
| 285 | msg = json.dumps(data, cls=ObjectJSONEncoder) |
| 286 | return msg |