Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to customize log format #1197

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion supervisor/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ class ServerOptions(Options):
sockchmod = None
logfile = None
loglevel = None
logformat = None
pidfile = None
passwdfile = None
nodaemon = None
Expand Down Expand Up @@ -431,6 +432,8 @@ def __init__(self):
"z:", "logfile_backups=", integer, default=10)
self.add("loglevel", "supervisord.loglevel", "e:", "loglevel=",
logging_level, default="info")
self.add("logformat", "supervisord.logformat", "", "logformat=",
str, default='%(asctime)s %(levelname)s %(message)s')
self.add("pidfile", "supervisord.pidfile", "j:", "pidfile=",
existing_dirpath, default="supervisord.pid")
self.add("identifier", "supervisord.identifier", "i:", "identifier=",
Expand Down Expand Up @@ -493,6 +496,9 @@ def realize(self, *arg, **kw):
if not self.loglevel:
self.loglevel = section.loglevel

if not self.logformat:
self.logformat = section.logformat

if self.logfile:
logfile = self.logfile
else:
Expand Down Expand Up @@ -635,6 +641,7 @@ def get(opt, default, **kwargs):
section.logfile_maxbytes = byte_size(get('logfile_maxbytes', '50MB'))
section.logfile_backups = integer(get('logfile_backups', 10))
section.loglevel = logging_level(get('loglevel', 'info'))
section.logformat = str(get('logformat', '%(asctime)s %(levelname)s %(message)s', do_expand=False))
section.pidfile = existing_dirpath(get('pidfile', 'supervisord.pid'))
section.identifier = get('identifier', 'supervisor')
section.nodaemon = boolean(get('nodaemon', 'false'))
Expand Down Expand Up @@ -1447,7 +1454,7 @@ def set_rlimits_or_exit(self):

def make_logger(self):
# must be called after realize() and after supervisor does setuid()
format = '%(asctime)s %(levelname)s %(message)s\n'
format = self.logformat+'\n'
self.logger = loggers.getLogger(self.loglevel)
if self.nodaemon:
loggers.handle_stdout(self.logger, format)
Expand Down
3 changes: 3 additions & 0 deletions supervisor/tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ def test_options(self):
logfile_maxbytes=1000MB
logfile_backups=5
loglevel=error
logformat=%%(asctime)s | %%(levelname)s | %%(message)s
pidfile=supervisord.pid
nodaemon=true
identifier=fleeb
Expand Down Expand Up @@ -513,6 +514,7 @@ def test_options(self):
self.assertEqual(options.logfile_maxbytes, 1000 * 1024 * 1024)
self.assertEqual(options.logfile_backups, 5)
self.assertEqual(options.loglevel, 40)
self.assertEqual(options.logformat, '%(asctime)s | %(levelname)s | %(message)s')
self.assertEqual(options.pidfile, 'supervisord.pid')
self.assertEqual(options.nodaemon, True)
self.assertEqual(options.identifier, 'fleeb')
Expand Down Expand Up @@ -662,6 +664,7 @@ def test_options(self):
self.assertEqual(instance.logfile_maxbytes, 1000 * 1024 * 1024)
self.assertEqual(instance.logfile_backups, 5)
self.assertEqual(instance.loglevel, 40)
self.assertEqual(instance.logformat, '%(asctime)s | %(levelname)s | %(message)s')
self.assertEqual(instance.pidfile, os.path.join(here,'supervisord.pid'))
self.assertEqual(instance.nodaemon, True)
self.assertEqual(instance.passwdfile, None)
Expand Down