- commit
- 43624c0386c08ec1269c9077f0865dc45e45acff
- parent
- cd2cdb0ba04c40ea99de0b754a5bb8096bb2cc4d
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2023-10-16 09:28
correct some factual erros in loging post
Diffstat
M | _content/posts/2023-10-13-logging/index.md | 26 | +++++++++++++++++--------- |
1 files changed, 17 insertions, 9 deletions
diff --git a/_content/posts/2023-10-13-logging/index.md b/_content/posts/2023-10-13-logging/index.md
@@ -37,8 +37,8 @@ make sure that log records are written to stderr. 37 37 38 38 # Configuring handlers 39 3940 -1 By default, log records don't go anywhere. To make them do something useful,41 -1 you have to configure handlers.-1 40 Log records are sent to stderr by default. In production settings you usually -1 41 want something more involved, so you have to configure handlers. 42 42 43 43 `logging` provides many useful handlers such as `StreamHandler` to write to 44 44 stderr or `SMTPHandler` to send mails. Many more handlers are available from @@ -62,9 +62,11 @@ formatter = logging.Formatter( 62 62 '%(asctime)s %(levelname)-8s %(name)s %(message)s' 63 63 ) 64 64 -1 65 logging.root.setLevel(logging.NOTSET) -1 66 65 67 streamhandler = logging.StreamHandler()66 -1 streamhandler.level = logging._checkLevel(logging.INFO)67 -1 streamhandler.formatter = formatter-1 68 streamhandler.setLevel(logging.INFO) -1 69 streamhandler.setFormatter(formatter) 68 70 logging.root.addHandler(streamhandler) 69 71 70 72 smtphandler = SMTPHandler( @@ -73,8 +75,8 @@ smtphandler = SMTPHandler( 73 75 'you@localhost', 74 76 'new log message', 75 77 )76 -1 smtphandler.level = logging._checkLevel(logging.ERROR)77 -1 smtphandler.formatter = formatter-1 78 smtphandler.setLevel(logging.ERROR) -1 79 smtphandler.setFormatter(formatter) 78 80 logging.root.addHandler(smtphandler) 79 81 ``` 80 82 @@ -113,6 +115,7 @@ logging.config.dictConfig({ 113 115 }, 114 116 'root': { 115 117 'handlers': ['console', 'mail'], -1 118 'level': 'NOTSET', 116 119 }, 117 120 }) 118 121 ``` @@ -151,6 +154,7 @@ logging.config.dictConfig({ 151 154 }, 152 155 'root': { 153 156 'handlers': ['console'], -1 157 'level': 'NOTSET', 154 158 }, 155 159 }) 156 160 ``` @@ -164,12 +168,15 @@ instead of stderr. 164 168 how to do simple things. So here are some recommendations: 165 169 166 170 - Attach handlers to the root logger.167 -1 - Use `basicConfig()` (without arguments) if you just want to log to stderr.168 -1 - For anything more involved, use `dictConfig()`. It gives you full control169 -1 and since the dict can be encoded as JSON it can be used in any setup.-1 171 - Use `dictConfig()`, as it gives you full control and can be used in any -1 172 setup. 170 173 - It is theoretically possible to set a minimum level for both loggers and 171 174 handlers. I can't think of any case where a minimum level on a logger is 172 175 useful, so you should stick to using it on handlers. -1 176 - All loggers and handlers are initialized with no minimum level (i.e. -1 177 `NOTSET`). The only exception is the root logger which has a minimum level -1 178 of `WARNING`. To avoid confusion, I strongly recommend to reset that to -1 179 `NOTSET`. 173 180 174 181 # Wishful thinking 175 182 @@ -177,6 +184,7 @@ With that said, there are some things in `logging` that I wish were different: 177 184 178 185 - All handlers should accept `level` and `formatter` as keyword arguments. 179 186 The current way to construct them is just awkward. -1 187 - The root logger should have the `NOTSET` level. 180 188 - The default format should include the time. A log message without the time 181 189 is pretty much useless. Sometimes your logging pipeline will attach a time 182 190 automatically (e.g. journald). But for all other cases you basically have