]>
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
,
43 class InfoFilter(logging
.Filter
):
44 def filter(self
, rec
):
45 return rec
.levelno
in (logging
.DEBUG
, logging
.INFO
)
49 # Logger class definition
55 Logger class that encapsulates logging functions, internaly it uses Python
56 logging module with a separated instance instead of global.
58 Default logging level is 'info'.
62 # Create default global logger
63 self
.log_level
= logging
.INFO
64 self
.logger
= logging
.Logger("topolog", level
=self
.log_level
)
66 handler_stdout
= logging
.StreamHandler(sys
.stdout
)
67 handler_stdout
.setLevel(logging
.DEBUG
)
68 handler_stdout
.addFilter(InfoFilter())
69 handler_stdout
.setFormatter(
70 logging
.Formatter(fmt
="%(asctime)s %(levelname)s: %(message)s")
72 handler_stderr
= logging
.StreamHandler()
73 handler_stderr
.setLevel(logging
.WARNING
)
74 handler_stderr
.setFormatter(
75 logging
.Formatter(fmt
="%(asctime)s %(levelname)s: %(message)s")
78 self
.logger
.addHandler(handler_stdout
)
79 self
.logger
.addHandler(handler_stderr
)
82 self
.loggers
= {"topolog": self
.logger
}
84 def set_log_level(self
, level
):
85 "Set the logging level"
86 self
.log_level
= DEBUG_TOPO2LOGGING
.get(level
)
87 self
.logger
.setLevel(self
.log_level
)
89 def get_logger(self
, name
="topolog", log_level
=None, target
=sys
.stdout
):
91 Get a new logger entry. Allows creating different loggers for formating,
92 filtering or handling (file, stream or stdout/stderr).
95 log_level
= self
.log_level
96 if name
in self
.loggers
:
97 return self
.loggers
[name
]
99 nlogger
= logging
.Logger(name
, level
=log_level
)
100 if isinstance(target
, str):
101 handler
= logging
.FileHandler(filename
=target
)
103 handler
= logging
.StreamHandler(stream
=target
)
105 handler
.setFormatter(
106 logging
.Formatter(fmt
="%(asctime)s %(levelname)s: %(message)s")
108 nlogger
.addHandler(handler
)
109 self
.loggers
[name
] = nlogger
117 logger_config
= Logger()
118 logger
= logger_config
.logger