Logging¤
ASAB logging is built on top of the logging
module.
It provides standard logging to stderr
output when running on a console, and also file and syslog output (both RFC5424 and RFC3164) for background mode of operations.
Log timestamps are captured with sub-second precision (depending on the system capabilities) and displayed including microsecond part.
Recommended use¤
We recommend to create a logger L
in every module that captures all
necessary logging output. Alternative logging strategies are also
supported.
Example
The following code
will produce the following output to the console:
Logging Levels¤
ASAB uses Python logging levels with the addition of LOG_NOTICE
level.
LOG_NOTICE
level is similar to logging.INFO
level but it is visible
in even in non-verbose mode.
Level | Numeric value | Syslog Severity level |
---|---|---|
CRITICAL |
50 | Critical / crit / 2 |
ERROR |
40 | Error / err / 3 |
WARNING |
30 | Warning / warning / 4 |
LOG_NOTICE |
25 | Notice / notice / 5 |
INFO |
20 | Informational / info / 6 |
DEBUG |
10 | Debug / debug / 7 |
NOTSET |
0 |
Usage of the LOG_NOTICE
level:
Setting levels for particular modules:
Each logger must be on a separate line. The logger name and the corresponding logging level have to be separated by a space.
Verbose mode¤
By default, only logs with a level greater than 20 are visible, so DEBUG
and INFO
levels are not displayed. For accessing these levels, the application must be started with the verbose mode. This is done by the command-line argument -v
.
Example
There are three ways to display DEBUG
and INFO
log messages.
-
By enabling the verbose mode with command-line argument:
-
By accessing the
asab.Config["logging"]["verbose"]
boolean option:The string
true
is interpreted byConfigParser
viathis method
. -
By setting the logging level directly:
Note
The verbose mode also enables asyncio
and Zookeeper debug logging.
Structured data¤
ASAB supports a structured data to be added to a log entry. Structured data is passed to the log message as a dictionary that has to be JSON-serializable.
Example
The following warning message with structured data
will produce the following output to the console:
Info
ASAB structured data logging follows the RFC 5424.
Tip
Use logging with structured data to provide better error messages:
try:
print(a / b)
except ZeroDivisionError:
L.error("Division by zero.", struct_data={"a": a, "b": b})
Output:
Logging to console¤
The ASAB application will log to the console only if it detects that it is running on the terminal
(using os.isatty()
function) or if the
environment variable ASABFORCECONSOLE
is set to 1
. This is useful setup for eg. PyCharm.
Logging to a file¤
The command-line argument -l
enables logging to a file.
Non-empty path
option in the section [logging:file]
of the configuration file also enables logging to a file.
Example of the configuration section:
Tip
When the deployment expects more instances of the same application to be
logging into the same file, it is recommended to use
${HOSTNAME}
variable in the file path:
In this way, the applications will log to separate log files in different folders, which is an intended behavior, since race conditions may occur when different application instances log into the same file.
Log rotation¤
ASAB supports a log rotation.
The log rotation is triggered by a UNIX signal SIGHUP
, which can be used e.g. to integrate with logrotate
utility.
It is implemented using logging.handlers.RotatingFileHandler
from the Python standard library.
Also, a time-based log rotation can be configured using rotate_every
option.
Option | Meaning |
---|---|
backup_count |
A number of old files to be kept prior their removal. The system will save old log files by appending the extensions '.1', '.2' etc., to the filename. |
rotate_every |
Time interval of a log rotation. Default value is empty string, which means that the time-based log rotation is disabled. The interval is specified by an integer value and an unit, e.g. 1d (for 1 day) or 30M (30 minutes). Known units are H for hours, M for minutes, d for days and s for seconds. |
Logging to syslog¤
The command-line argument -s
enables logging to syslog.
A configuration section [logging:syslog]
can be used to specify
details about desired syslog logging.
Example of the configuration file section:
-
enabled
is equivalent to command-line switch-s
and it enables syslog logging target. -
format
specifies which logging format will be used.
Option | Format | Note |
---|---|---|
5 |
syslog format RFC 5424 | |
5micro |
syslog format RFC 5424 with microseconds | |
3 |
the old BSD syslog format RFC 3164 | It is typically used by /dev/log . |
m |
Mac OSX syslog flavour | It is based on BSD syslog format but it is not fully compatible. |
The default value is 3
on Linux and m
on Mac OSX.
-
address
specifies the location of the Syslog server. It could be a UNIX path such as/dev/log
or URL. Possible URL values: -
tcp://syslog.server.lan:1554/
for Syslog over TCP udp://syslog.server.lan:1554/
for Syslog over UDPunix-connect:///path/to/syslog.socket
for Syslog over UNIX socket (stream)unix-sendto:///path/to/syslog.socket
for Syslog over UNIX socket (datagram), equivalent to/path/to/syslog.socket
, used by a/dev/log
.
The default value is a /dev/log
on Linux or /var/run/syslog
on Mac OSX.
Logging of obsolete features¤
It proved to be essential to inform operators about features that are going to be obsoleted.
ASAB offers the unified asab.LogObsolete
logger which can indicate that a particular feature is marked as obsolete thru logs.
Such a log message can then be grepped from logs uniformly.
Example
produces the output:
We suggest to add the eol
attribute in the struct_data
of the log with a YYYY-MM-DD
date/time of the planned end of life of the feature.
Tip
We suggest automating the detection of obsolete warnings in logs so that the operations are informed well ahead of the actual removal of the feature. The string to seek in logs is "OBSOLETE".
Reference¤
Command-line arguments¤
Argument | Action |
---|---|
-v |
Enables verbose logging so that DEBUG and INFO levels are visible. |
-s |
Enables logging to syslog. |
asab.log.LOG_NOTICE = 25
module-attribute
¤
Info log level that is visible in non-verbose mode. It should not be used for warnings and errors.
asab.log.Logging
¤
Bases: object
Source code in asab/log.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|