mirror of
https://github.com/Telecominfraproject/wlan-lanforge-scripts.git
synced 2025-11-01 19:28:00 +00:00
logger_example.py: Initial commit
Signed-off-by: Matthew Stidham <stidmatt@gmail.com>
This commit is contained in:
50
py-scripts/sandbox/logger_example.py
Normal file
50
py-scripts/sandbox/logger_example.py
Normal file
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import logging
|
||||
from random import choice
|
||||
|
||||
|
||||
class ContextFilter(logging.Filter):
|
||||
"""
|
||||
This is a filter which injects contextual information into the log.
|
||||
|
||||
Rather than use actual contextual information, we just use random
|
||||
data in this demo.
|
||||
"""
|
||||
|
||||
USERS = ['jim', 'fred', 'sheila']
|
||||
IPS = ['123.231.231.123', '127.0.0.1', '192.168.0.1']
|
||||
|
||||
def filter(self, record):
|
||||
|
||||
record.ip = choice(ContextFilter.IPS)
|
||||
record.user = choice(ContextFilter.USERS)
|
||||
return True
|
||||
|
||||
|
||||
def main():
|
||||
levels = (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL)
|
||||
logging.basicConfig(level=logging.DEBUG,
|
||||
format='%(asctime)-15s %(name)-5s %(levelname)-8s IP: %(ip)-15s User: %(user)-8s %(message)s')
|
||||
a1 = logging.getLogger('a.b.c')
|
||||
a2 = logging.getLogger('d.e.f')
|
||||
|
||||
logging.basicConfig(filename='example.log', level=logging.DEBUG)
|
||||
logging.warning('this is a tutorial')
|
||||
logging.info('It includes the basic logging functions')
|
||||
|
||||
logging.info("Don't include certain strings")
|
||||
logging.info('define a filter')
|
||||
f = ContextFilter()
|
||||
a1.addFilter(f)
|
||||
a2.addFilter(f)
|
||||
a1.debug('A debug message')
|
||||
a1.info('An info message with %s', 'some parameters')
|
||||
for x in range(10):
|
||||
lvl = choice(levels)
|
||||
lvlname = logging.getLevelName(lvl)
|
||||
a2.log(lvl, 'A message at %s level with %d %s', lvlname, 2, 'parameters')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user