Skip to content

Commit

Permalink
Add timestamp to structured log data
Browse files Browse the repository at this point in the history
  • Loading branch information
anodos325 committed Dec 31, 2024
1 parent dd461fb commit 642372e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/middlewared/middlewared/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from collections import deque
from json import dumps
from dataclasses import dataclass
from .utils.time_utils import utc_now

# markdown debug is also considered useless
logging.getLogger('MARKDOWN').setLevel(logging.INFO)
Expand Down Expand Up @@ -151,6 +152,7 @@ def format(self, record: logging.LogRecord) -> str:

if structured_data:
structured_data['type'] = 'PYTHON_EXCEPTION'
structured_data['time'] = utc_now().strftime('%Y-%m-%d %H:%M:%S.%f')
json_data = dumps({'TNLOG': structured_data})
msg += f' @cee:{json_data}'

Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,5 @@ def test__syslog_exception_parameterization(working_syslog, test_message):
exc = log_line.split('@cee:')[1]
data = json.loads(exc)
assert data['TNLOG']['type'] == 'PYTHON_EXCEPTION'
assert 'time' in data['TNLOG']
assert 'FileNotFoundError' in data['TNLOG']['exception']

0 comments on commit 642372e

Please sign in to comment.