summaryrefslogtreecommitdiff
path: root/ick2/logging.py
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2017-08-06 11:46:02 +0300
committerLars Wirzenius <liw@liw.fi>2017-08-06 18:56:34 +0300
commit6299228754893813341085d99c3924f7fefe1c18 (patch)
tree432e9f076b3b226487b8a77359545adba50e1714 /ick2/logging.py
parent888db73b93aefe70d838d499f7f9cc43eee7372b (diff)
downloadick2-6299228754893813341085d99c3924f7fefe1c18.tar.gz
Add: ControllerAPI, ControllerState
Diffstat (limited to 'ick2/logging.py')
-rw-r--r--ick2/logging.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/ick2/logging.py b/ick2/logging.py
new file mode 100644
index 0000000..729778d
--- /dev/null
+++ b/ick2/logging.py
@@ -0,0 +1,57 @@
+# Copyright (C) 2017 Lars Wirzenius
+
+
+import slog
+
+
+def drop_get_message(log_obj):
+ # These are useless and annoying in gunicorn log messages.
+ if 'getMessage' in log_obj:
+ del log_obj['getMessage']
+ return log_obj
+
+
+# We are probably run under gunicorn, which sets up logging via the
+# logging library. Hijack that so actual logging happens via the slog
+# library. For this, we need to know the logger names gunicorn uses.
+gunicorn_loggers = ['gunicorn.access', 'gunicorn.error']
+
+
+# This sets up a global log variable that doesn't actually log
+# anything anywhere. This is useful so that code can unconditionally
+# call log.log(...) from anywhere. See setup_logging() for setting up
+# actual logging to somewhere persistent.
+
+log = slog.StructuredLog()
+log.add_log_writer(slog.NullSlogWriter(), slog.FilterAllow())
+log.add_log_massager(drop_get_message)
+slog.hijack_logging(log, logger_names=gunicorn_loggers)
+
+
+def setup_logging(config):
+ if 'log' in config:
+ assert isinstance(config['log'], list)
+ for target in config.get('log', []):
+ setup_logging_to_target(target)
+
+
+def setup_logging_to_target(target):
+ rule = get_filter_rules(target)
+ if 'filename' in target:
+ setup_logging_to_file(target, rule)
+ else:
+ raise Exception('Do not understand logging target %r' % target)
+
+
+def get_filter_rules(target):
+ if 'filter' in target:
+ return slog.construct_log_filter(target['filter'])
+ return slog.FilterAllow()
+
+
+def setup_logging_to_file(target, rule):
+ writer = slog.FileSlogWriter()
+ writer.set_filename(target['filename'])
+ if 'max_bytes' in target:
+ writer.set_max_file_size(target['max_bytes'])
+ log.add_log_writer(writer, rule)