compute/logging.md
... ...
@@ -1 +1,27 @@
1
+# Logging
1 2
3
+There is only one acceptable log format. It is this:
4
+```
5
+<time> <level> [<application>] <message>
6
+```
7
+
8
+The application can be left out if you're logging to individual files.
9
+
10
+The time spec MUST be ISO8601. Nothing else is acceptable.
11
+The time spec MUST include timezone. Your servers of course should be in UTC, per [rule 8](rules.md).
12
+The time spec MUST NOT include spaces.
13
+The time precision is up to you. Maybe leave out milliseconds for general logging.
14
+
15
+Do NOT rotate your own logs - use logrotate. Sooner or later someone is going to rotate your logs forcefully anyway, so you might as well be set up for it.
16
+
17
+Here's the One True Logger in python, using WatchedFileHandler to allow for logrotate:
18
+```
19
+log_format = "%(asctime)s %(levelname)s %(message)s"
20
+date_format = "%Y-%m-%dT%H:%M:%S%Z"
21
+log_handler = logging.handlers.WatchedFileHandler(log_file)
22
+formatter = logging.Formatter(log_format, date_format)
23
+log_handler.setFormatter(formatter)
24
+logger = logging.getLogger()
25
+logger.addHandler(log_handler)
26
+logger.setLevel(level)
27
+```