3 """Utilities related to logging."""
8 from logging.handlers import RotatingFileHandler, SysLogHandler
15 import string_utils as su
16 import thread_utils as tu
18 parser = config.add_commandline_args(
19 f'Logging ({__file__})',
20 'Args related to logging')
22 '--logging_config_file',
23 type=argparse_utils.valid_filename,
26 help='Config file containing the logging setup, see: https://docs.python.org/3/howto/logging.html#logging-advanced-tutorial',
32 choices=['NOTSET', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
34 help='The level below which to squelch log messages.',
39 default='%(levelname)s:%(asctime)s: %(message)s',
40 help='The format for lines logged via the logger module.'
43 '--logging_date_format',
45 default='%Y/%m/%dT%H:%M:%S.%f%z',
47 help='The format of any dates in --logging_format.'
51 action=argparse_utils.ActionNoYes,
53 help='Should we log to the console (stderr)',
60 help='The filename of the logfile to write.'
63 '--logging_filename_maxsize',
67 help='The maximum size (in bytes) to write to the logging_filename.'
70 '--logging_filename_count',
74 help='The number of logging_filename copies to keep before deleting.'
78 action=argparse_utils.ActionNoYes,
80 help='Should we log to localhost\'s syslog.'
83 '--logging_debug_threads',
84 action=argparse_utils.ActionNoYes,
86 help='Should we prepend pid/tid data to all log messages?'
89 '--logging_info_is_print',
90 action=argparse_utils.ActionNoYes,
92 help='logging.info also prints to stdout.'
96 class OnlyInfoFilter(logging.Filter):
97 def filter(self, record):
98 return record.levelno == logging.INFO
101 class MillisecondAwareFormatter(logging.Formatter):
102 converter = datetime.datetime.fromtimestamp
104 def formatTime(self, record, datefmt=None):
105 ct = self.converter(record.created, pytz.timezone("US/Pacific"))
107 s = ct.strftime(datefmt)
109 t = ct.strftime("%Y-%m-%d %H:%M:%S")
110 s = "%s,%03d" % (t, record.msecs)
114 def initialize_logging(logger=None) -> logging.Logger:
115 assert config.has_been_parsed()
117 logger = logging.getLogger() # Root logger
119 if config.config['logging_config_file'] is not None:
120 logging.config.fileConfig('logging.conf')
124 numeric_level = getattr(
126 config.config['logging_level'].upper(),
129 if not isinstance(numeric_level, int):
130 raise ValueError('Invalid level: %s' % config.config['logging_level'])
132 fmt = config.config['logging_format']
133 if config.config['logging_debug_threads']:
134 fmt = f'%(process)d.%(thread)d|{fmt}'
136 if config.config['logging_syslog']:
137 if sys.platform in ('win32', 'cygwin'):
139 "WARNING: Current platform does not support syslog; IGNORING.",
143 handler = SysLogHandler()
144 # for k, v in encoded_priorities.items():
145 # handler.encodePriority(k, v)
146 handler.setFormatter(
147 MillisecondAwareFormatter(
149 datefmt=config.config['logging_date_format'],
152 handler.setLevel(numeric_level)
153 handlers.append(handler)
155 if config.config['logging_filename'] is not None:
156 handler = RotatingFileHandler(
157 config.config['logging_filename'],
158 maxBytes = config.config['logging_filename_maxsize'],
159 backupCount = config.config['logging_filename_count'],
161 handler.setLevel(numeric_level)
162 handler.setFormatter(
163 MillisecondAwareFormatter(
165 datefmt=config.config['logging_date_format'],
168 handlers.append(handler)
170 if config.config['logging_console']:
171 handler = logging.StreamHandler(sys.stderr)
172 handler.setLevel(numeric_level)
173 handler.setFormatter(
174 MillisecondAwareFormatter(
176 datefmt=config.config['logging_date_format'],
179 handlers.append(handler)
181 if len(handlers) == 0:
182 handlers.append(logging.NullHandler())
184 for handler in handlers:
185 logger.addHandler(handler)
186 if config.config['logging_info_is_print']:
187 handler = logging.StreamHandler(sys.stdout)
188 handler.addFilter(OnlyInfoFilter())
189 logger.addHandler(handler)
190 logger.setLevel(numeric_level)
191 logger.propagate = False
195 def get_logger(name: str = ""):
196 logger = logging.getLogger(name)
197 return initialize_logging(logger)
200 def tprint(*args, **kwargs) -> None:
201 if config.config['logging_debug_threads']:
202 print(f'{tu.current_thread_id()}', end="")
203 print(*args, **kwargs)
208 def dprint(*args, **kwargs) -> None:
209 print(*args, file=sys.stderr, **kwargs)
212 class OutputSink(object):
214 # Bits in the destination_bitv bitvector. Used to indicate the
215 # output destination.
220 LOG_WARNING = 0x10 # > Should provide logger to the c'tor.
222 LOG_CRITICAL = 0x40 # _/
223 FILENAME = 0x80 # Must provide a filename to the c'tor.
226 ALL_LOG_DESTINATIONS = (
227 LOG_DEBUG | LOG_INFO | LOG_WARNING | LOG_ERROR | LOG_CRITICAL
229 ALL_OUTPUT_DESTINATIONS = 0x1FF
232 destination_bitv: int,
237 logger = logging.getLogger(None)
240 if filename is not None:
241 self.f = open(filename, "wb", buffering=0)
243 if self.destination_bitv & OutputSink.FILENAME:
245 "Filename argument is required if bitv & FILENAME"
248 self.set_destination_bitv(destination_bitv)
250 def get_destination_bitv(self):
251 return self.destination_bitv
253 def set_destination_bitv(self, destination_bitv: int):
254 if destination_bitv & self.FILENAME and self.f is None:
256 "Filename argument is required if bitv & FILENAME"
258 self.destination_bitv = destination_bitv
260 def print(self, *args, **kwargs):
261 end = kwargs.pop("end", None)
263 if not isinstance(end, str):
264 raise TypeError("end must be None or a string")
265 sep = kwargs.pop("sep", None)
267 if not isinstance(sep, str):
268 raise TypeError("sep must be None or a string")
270 raise TypeError("invalid keyword arguments to print()")
271 buf = su.sprintf(*args, end="", sep=sep)
276 if self.destination_bitv & self.STDOUT:
277 print(buf, file=sys.stdout, sep=sep, end=end)
278 if self.destination_bitv & self.STDERR:
279 print(buf, file=sys.stderr, sep=sep, end=end)
282 if self.destination_bitv & self.FILENAME and self.f is not None:
283 self.f.write(buf.encode('utf-8'))
285 buf = su.strip_escape_sequences(buf)
286 if self.logger is not None:
287 if self.destination_bitv & self.LOG_DEBUG:
288 self.logger.debug(buf)
289 if self.destination_bitv & self.LOG_INFO:
290 self.logger.info(buf)
291 if self.destination_bitv & self.LOG_WARNING:
292 self.logger.warning(buf)
293 if self.destination_bitv & self.LOG_ERROR:
294 self.logger.error(buf)
295 if self.destination_bitv & self.LOG_CRITICAL:
296 self.logger.critical(buf)
297 if self.destination_bitv & self.HLOG:
301 if self.f is not None:
305 class OutputContext(OutputSink, contextlib.ContextDecorator):
307 destination_bitv: int,
311 super().__init__(destination_bitv, logger=logger, filename=filename)
316 def __exit__(self, etype, value, traceback):
318 if etype is not None:
323 def hlog(message: str) -> None:
324 message = message.replace("'", "'\"'\"'")
325 os.system(f"/usr/bin/logger -p local7.info -- '{message}'")