Skip to content

Commit 4628b58

Browse files
committed
Common options for daemons.
1 parent 2283600 commit 4628b58

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

simpledaemonlog/daemonarguments.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""daemonarguments.py: Common options for daemons with argconfparse and simplelogging."""
2+
3+
import simplelogging.logsetup
4+
from argconfparse.argconfparse import arg_str2bool
5+
6+
7+
__author__ = 'Raido Pahtma'
8+
__license__ = "MIT"
9+
10+
11+
def add_daemon_arguments(parser):
12+
parser.add_argument("--daemon", default=False, type=arg_str2bool, help="Daemon mode, no logging to console.")
13+
parser.add_argument("-d", dest="daemon", action="store_true", help="Daemon mode, no logging to console.")
14+
parser.add_argument("--logdir", default=None, help="Folder where logfiles should be stored.")
15+
parser.add_argument("--colorlog", default=False, type=arg_str2bool, help="Color logs for terminal output.")
16+
17+
18+
def setup_daemon(args, name):
19+
if args.daemon is False:
20+
simplelogging.logsetup.setup_console(color=args.colorlog)
21+
22+
if args.logdir is not None:
23+
simplelogging.logsetup.setup_file(name, logdir=args.logdir)
24+
elif args.daemon:
25+
print "WARNING Logging not configured!"
26+
print args

simpledaemonlog/logsetup.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
"""logsetup.py: A simple python logging setup for console and local log files."""
22

3-
__author__ = 'Raido Pahtma'
4-
__license__ = "MIT"
5-
63
import os
74
import time
85
import sys
9-
import logging
106
import logging.config
117
import yaml
128

139
import logging
1410
log = logging.getLogger(__name__)
1511

12+
13+
__author__ = 'Raido Pahtma'
14+
__license__ = "MIT"
15+
16+
1617
DEFAULT_FORMAT_STRING = '%(asctime)s|%(levelname)8s|%(module)20s|%(lineno)4s| %(message)s'
1718
COLORED_FORMAT_STRING = '%(log_color)s%(asctime)s%(reset)s|%(module)20s|%(lineno)4s| %(log_color)s%(message)s'
1819

@@ -137,4 +138,4 @@ def setup_file(application_name, logdir="log", level=logging.NOTSET, fs=DEFAULT_
137138
except TypeError:
138139
log.exception("The description of an exception")
139140

140-
print("The fine print")
141+
print("The fine print")

0 commit comments

Comments
 (0)