Skip to content

Commit

Permalink
Merge pull request #1301 from terev/master
Browse files Browse the repository at this point in the history
Flag to disable stdout logging for non daemonized instances
  • Loading branch information
mnaberez committed Dec 8, 2019
2 parents 7be0cde + 08399a0 commit 3e7c082
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,16 @@ follows.

*Introduced*: 3.0

``silent``

If true and not daemonized, logs will not be directed to stdout.

*Default*: false

*Required*: No.

*Introduced*: 4.2.0

``minfds``

The minimum number of file descriptors that must be available before
Expand Down
4 changes: 4 additions & 0 deletions docs/running.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ value in the configuration file.

Run :program:`supervisord` in the foreground.

-s, --silent

No output directed to stdout.

-h, --help

Show :command:`supervisord` command help.
Expand Down
6 changes: 5 additions & 1 deletion supervisor/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ class ServerOptions(Options):
pidfile = None
passwdfile = None
nodaemon = None
silent = None
environment = None
httpservers = ()
unlink_pidfile = False
Expand Down Expand Up @@ -447,6 +448,8 @@ def __init__(self):
"t", "strip_ansi", flag=1, default=0)
self.add("profile_options", "supervisord.profile_options",
"", "profile_options=", profile_options, default=None)
self.add("silent", "supervisord.silent",
"s", "silent", flag=1, default=0)
self.pidhistory = {}
self.process_group_configs = []
self.parse_criticals = []
Expand Down Expand Up @@ -638,6 +641,7 @@ def get(opt, default, **kwargs):
section.pidfile = existing_dirpath(get('pidfile', 'supervisord.pid'))
section.identifier = get('identifier', 'supervisor')
section.nodaemon = boolean(get('nodaemon', 'false'))
section.silent = boolean(get('silent', 'false'))

tempdir = tempfile.gettempdir()
section.childlogdir = existing_directory(get('childlogdir', tempdir))
Expand Down Expand Up @@ -1461,7 +1465,7 @@ def make_logger(self):
# must be called after realize() and after supervisor does setuid()
format = '%(asctime)s %(levelname)s %(message)s\n'
self.logger = loggers.getLogger(self.loglevel)
if self.nodaemon:
if self.nodaemon and not self.silent:
loggers.handle_stdout(self.logger, format)
loggers.handle_file(
self.logger,
Expand Down
1 change: 1 addition & 0 deletions supervisor/skel/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ logfile_backups=10 ; # of main logfile backups; 0 means none, default
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
silent=false ; no logs to stdout if true; default false
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200
;umask=022 ; process file creation umask; default 022
Expand Down
1 change: 1 addition & 0 deletions supervisor/supervisord.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Options:
-c/--configuration FILENAME -- configuration file path (searches if not given)
-n/--nodaemon -- run in the foreground (same as 'nodaemon=true' in config file)
-s/--silent -- no logs to stdout (maps to 'silent=true' in config file)
-h/--help -- print this usage message and exit
-v/--version -- print supervisord version number and exit
-u/--user USER -- run supervisord as this user (or numeric uid)
Expand Down
1 change: 1 addition & 0 deletions supervisor/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def __init__(self):
self.chdir_error = None
self.umaskset = None
self.poller = DummyPoller(self)
self.silent = False

def getLogger(self, *args, **kw):
logger = DummyLogger()
Expand Down
5 changes: 5 additions & 0 deletions supervisor/tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ def test_options(self):
loglevel=error
pidfile=supervisord.pid
nodaemon=true
silent=true
identifier=fleeb
childlogdir=%(tempdir)s
nocleanup=true
Expand Down Expand Up @@ -539,6 +540,7 @@ def test_options(self):
self.assertEqual(options.loglevel, 40)
self.assertEqual(options.pidfile, 'supervisord.pid')
self.assertEqual(options.nodaemon, True)
self.assertEqual(options.silent, True)
self.assertEqual(options.identifier, 'fleeb')
self.assertEqual(options.childlogdir, tempfile.gettempdir())
self.assertEqual(len(options.server_configs), 1)
Expand Down Expand Up @@ -688,6 +690,7 @@ def test_options(self):
self.assertEqual(instance.loglevel, 40)
self.assertEqual(instance.pidfile, os.path.join(here,'supervisord.pid'))
self.assertEqual(instance.nodaemon, True)
self.assertEqual(instance.silent, True)
self.assertEqual(instance.passwdfile, None)
self.assertEqual(instance.identifier, 'fleeb')
self.assertEqual(instance.childlogdir, tempfile.gettempdir())
Expand Down Expand Up @@ -1817,6 +1820,7 @@ def test_options_with_environment_expansions(self):
'ENV_SUPD_LOGFILE_BACKUPS': '10',
'ENV_SUPD_LOGLEVEL': 'info',
'ENV_SUPD_NODAEMON': 'false',
'ENV_SUPD_SILENT': 'false',
'ENV_SUPD_MINFDS': '1024',
'ENV_SUPD_MINPROCS': '200',
'ENV_SUPD_UMASK': '002',
Expand Down Expand Up @@ -1852,6 +1856,7 @@ def test_options_with_environment_expansions(self):
self.assertEqual(instance.logfile_backups, 10)
self.assertEqual(instance.loglevel, LevelsByName.INFO)
self.assertEqual(instance.nodaemon, False)
self.assertEqual(instance.silent, False)
self.assertEqual(instance.minfds, 1024)
self.assertEqual(instance.minprocs, 200)
self.assertEqual(instance.nocleanup, True)
Expand Down
42 changes: 42 additions & 0 deletions supervisor/tests/test_supervisord.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,48 @@ def test_main_profile(self):
output = new_stdout.getvalue()
self.assertTrue('cumulative time, call count' in output, output)

def test_silent_off(self):
from supervisor.supervisord import main
conf = os.path.join(
os.path.abspath(os.path.dirname(__file__)), 'fixtures',
'donothing.conf')
new_stdout = StringIO()
new_stdout.fileno = lambda: 1
old_stdout = sys.stdout

try:
tempdir = tempfile.mkdtemp()
log = os.path.join(tempdir, 'log')
pid = os.path.join(tempdir, 'pid')
sys.stdout = new_stdout
main(args=['-c', conf, '-l', log, '-j', pid, '-n'], test=True)
finally:
sys.stdout = old_stdout
shutil.rmtree(tempdir)
output = new_stdout.getvalue()
self.assertGreater(len(output), 0)

def test_silent_on(self):
from supervisor.supervisord import main
conf = os.path.join(
os.path.abspath(os.path.dirname(__file__)), 'fixtures',
'donothing.conf')
new_stdout = StringIO()
new_stdout.fileno = lambda: 1
old_stdout = sys.stdout

try:
tempdir = tempfile.mkdtemp()
log = os.path.join(tempdir, 'log')
pid = os.path.join(tempdir, 'pid')
sys.stdout = new_stdout
main(args=['-c', conf, '-l', log, '-j', pid, '-n', '-s'], test=True)
finally:
sys.stdout = old_stdout
shutil.rmtree(tempdir)
output = new_stdout.getvalue()
self.assertEqual(len(output), 0)

class SupervisordTests(unittest.TestCase):
def tearDown(self):
from supervisor.events import clear
Expand Down

0 comments on commit 3e7c082

Please sign in to comment.