]>
git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/lib/topolog.py
3 # Library of helper functions for NetDEF Topology Tests
5 # Copyright (c) 2017 by
6 # Network Device Education Foundation, Inc. ("NetDEF")
8 # Permission to use, copy, modify, and/or distribute this software
9 # for any purpose with or without fee is hereby granted, provided
10 # that the above copyright notice and this permission notice appear
13 # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
14 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
16 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
17 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 Logging utilities for topology tests.
26 This file defines our logging abstraction.
32 # Helper dictionary to convert Topogen logging levels to Python's logging.
33 DEBUG_TOPO2LOGGING
= {
34 'debug': logging
.DEBUG
,
36 'output': logging
.INFO
,
37 'warning': logging
.WARNING
,
38 'error': logging
.ERROR
,
39 'critical': logging
.CRITICAL
,
42 class InfoFilter(logging
.Filter
):
43 def filter(self
, rec
):
44 return rec
.levelno
in (logging
.DEBUG
, logging
.INFO
)
47 # Logger class definition
52 Logger class that encapsulates logging functions, internaly it uses Python
53 logging module with a separated instance instead of global.
55 Default logging level is 'info'.
59 # Create default global logger
60 self
.log_level
= logging
.INFO
61 self
.logger
= logging
.Logger('topolog', level
=self
.log_level
)
63 handler_stdout
= logging
.StreamHandler(sys
.stdout
)
64 handler_stdout
.setLevel(logging
.DEBUG
)
65 handler_stdout
.addFilter(InfoFilter())
66 handler_stdout
.setFormatter(
67 logging
.Formatter(fmt
='%(asctime)s %(levelname)s: %(message)s')
69 handler_stderr
= logging
.StreamHandler()
70 handler_stderr
.setLevel(logging
.WARNING
)
71 handler_stderr
.setFormatter(
72 logging
.Formatter(fmt
='%(asctime)s %(levelname)s: %(message)s')
75 self
.logger
.addHandler(handler_stdout
)
76 self
.logger
.addHandler(handler_stderr
)
79 self
.loggers
= {'topolog': self
.logger
}
81 def set_log_level(self
, level
):
82 "Set the logging level"
83 self
.log_level
= DEBUG_TOPO2LOGGING
.get(level
)
84 self
.logger
.setLevel(self
.log_level
)
86 def get_logger(self
, name
='topolog', log_level
=None, target
=sys
.stdout
):
88 Get a new logger entry. Allows creating different loggers for formating,
89 filtering or handling (file, stream or stdout/stderr).
92 log_level
= self
.log_level
93 if self
.loggers
.has_key(name
):
94 return self
.loggers
[name
]
96 nlogger
= logging
.Logger(name
, level
=log_level
)
97 if isinstance(target
, str):
98 handler
= logging
.FileHandler(filename
=target
)
100 handler
= logging
.StreamHandler(stream
=target
)
102 handler
.setFormatter(
103 logging
.Formatter(fmt
='%(asctime)s %(levelname)s: %(message)s')
105 nlogger
.addHandler(handler
)
106 self
.loggers
[name
] = nlogger
113 logger_config
= Logger()
114 logger
= logger_config
.logger