2 # -*- coding: utf-8 -*-
4 # © Copyright 2021-2023, Scott Gasch
7 This is a module that offers an opinionated take on how whole program
8 logging should be initialized and controlled. It uses the standard
9 Python :mod:`logging` but gives you control, via commandline config,
12 * Set the logging default level (debug, info, warning, error, critical)
13 of the whole program (see: :code:`--logging_level`)... and to override
14 the logging level with :class:`LoggingContext`.
15 * Prepend or append a message to every log record also with
16 :class:`LoggingContext`.
17 * define the logging message format (see :code:`--logging_format` and
18 :code:`--logging_date_format`) including easily adding a PID/TID
19 marker on all messages to help with multithreaded debugging
20 (:code:`--logging_debug_threads`) and force module names of code
21 that emits log messages to be included in the format
22 (:code:`--logging_debug_modules`),
23 * control the destination of logged messages:
25 - log to the console/stderr (:code:`--logging_console`) and/or
26 - log to a rotated file (:code:`--logging_filename`,
27 :code:`--logging_filename_maxsize` and :code:`--logging_filename_count`)
29 - log to the UNIX syslog (:code:`--logging_syslog` and
30 :code:`--logging_syslog_facility`)
32 * optionally squelch repeated messages (:code:`--logging_squelch_repeats`),
33 * optionally log probalistically (:code:`--logging_probabilistically`),
34 * capture printed messages into the info log (:code:`--logging_captures_prints`),
35 * and optionally clear unwanted logging handlers added by other imports
36 before this one (:code:`--logging_clear_preexisting_handlers`).
37 * There are also :class:`LoggerAdapter` classes to implement prefix/suffix
38 functionality without using :class:`LoggingContext` by wrapping the
41 To use this functionality, call :meth:`initialize_logging` early
42 in your program entry point. If you use the
43 :meth:`pyutils.bootstrap.initialize` decorator on your program's entry
44 point, it will call this for you automatically.
55 from logging.config import fileConfig
56 from logging.handlers import RotatingFileHandler, SysLogHandler
57 from typing import Any, Callable, Dict, Iterable, List, Optional, Union
60 from overrides import overrides
62 # This module is commonly used by others in here and should avoid
63 # taking any unnecessary dependencies back on them.
64 from pyutils import argparse_utils, config, misc_utils
66 cfg = config.add_commandline_args(f"Logging ({__file__})", "Args related to logging")
68 "--logging_config_file",
69 type=argparse_utils.valid_filename,
72 help="Config file containing the logging setup, see: https://docs.python.org/3/howto/logging.html#logging-advanced-tutorial",
78 choices=["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
80 help="The global default level below which to squelch log messages",
86 help="The format for lines logged via the logger module. See: https://docs.python.org/3/library/logging.html#formatter-objects",
89 "--logging_date_format",
91 default="%Y/%m/%dT%H:%M:%S.%f%z",
93 help="The format of any dates in --logging_format.",
97 action=argparse_utils.ActionNoYes,
99 help="Should we log to the console (stderr)",
102 "--logging_filename",
106 help="The filename of the logfile to write.",
109 "--logging_filename_maxsize",
111 default=(1024 * 1024),
113 help="The maximum size (in bytes) to write to the logging_filename.",
116 "--logging_filename_count",
120 help="The number of logging_filename copies to keep before deleting.",
124 action=argparse_utils.ActionNoYes,
126 help="Should we log to localhost's syslog.",
129 "--logging_syslog_facility",
155 metavar="SYSLOG_FACILITY_LIST",
156 help="The default syslog message facility identifier",
159 "--logging_debug_threads",
160 action=argparse_utils.ActionNoYes,
162 help="Should we prepend pid/tid data to all log messages?",
165 "--logging_debug_modules",
166 action=argparse_utils.ActionNoYes,
168 help="Should we prepend module/function data to all log messages?",
171 "--logging_info_is_print",
172 action=argparse_utils.ActionNoYes,
174 help="logging.info also prints to stdout.",
177 "--logging_squelch_repeats",
178 action=argparse_utils.ActionNoYes,
180 help="Do we allow code to indicate that it wants to squelch repeated logging messages or should we always log?",
183 "--logging_probabilistically",
184 action=argparse_utils.ActionNoYes,
186 help="Do we allow probabilistic logging (for code that wants it) or should we always log?",
188 # See also: OutputMultiplexer
190 "--logging_captures_prints",
191 action=argparse_utils.ActionNoYes,
193 help="When calling print, also log.info automatically.",
196 "--logging_clear_preexisting_handlers",
197 action=argparse_utils.ActionNoYes,
200 "Should logging code clear preexisting global logging handlers and thus insist that is "
201 + "alone can add handlers. Use this to work around annoying modules that insert global "
202 + "handlers with formats and logging levels you might now want. Caveat emptor, this may "
203 + "cause you to miss logging messages."
207 BUILT_IN_PRINT = print
208 LOGGING_INITIALIZED = False
211 # A map from logging_callsite_id -> count of logged messages.
212 squelched_logging_counts: Dict[str, int] = {}
215 def squelch_repeated_log_messages(squelch_after_n_repeats: int) -> Callable:
217 A decorator that marks a function as interested in having the logging
218 messages that it produces be squelched (ignored) after it logs the
219 same message more than N times.
223 This decorator affects *ALL* logging messages produced
224 within the decorated function. That said, messages must be
225 identical in order to be squelched. For example, if the same line
226 of code produces different messages (because of, e.g., a format
227 string), the messages are considered to be different.
229 An example of this from the pyutils code itself can be found in
230 :meth:`pyutils.ansi.fg` and :meth:`pyutils.ansi.bg` methods::
232 @logging_utils.squelch_repeated_log_messages(1)
234 name: Optional[str] = "",
235 red: Optional[int] = None,
236 green: Optional[int] = None,
237 blue: Optional[int] = None,
239 force_16color: bool = False,
240 force_216color: bool = False,
244 These methods log stuff like "Using 24-bit color strategy" which
245 gets old really fast and fills up the logs. By decorating the methods
246 with :code:`@logging_utils.squelch_repeated_log_messages(1)` the code
247 is requesting that its logged messages be dropped silently after the
248 first one is produced (note the argument 1).
250 Users can insist that all logged messages always be reflected in the
251 logs using the :code:`--no_logging_squelch_repeats` flag but the default
252 behavior is to allow code to request it be squelched.
254 :code:`--logging_squelch_repeats` only affects code with this decorator
255 on it; it ignores all other code.
258 squelch_after_n_repeats: the number of repeated messages allowed to
259 log before subsequent messages are silently dropped.
262 def squelch_logging_wrapper(f: Callable):
263 from pyutils import function_utils
265 identifier = function_utils.function_identifier(f)
266 squelched_logging_counts[identifier] = squelch_after_n_repeats
269 return squelch_logging_wrapper
272 class SquelchRepeatedMessagesFilter(logging.Filter):
273 """A filter that only logs messages from a given site with the same
274 (exact) message at the same logging level N times and ignores
275 subsequent attempts to log.
277 This filter only affects logging messages that repeat more than a
278 threshold number of times from functions that are tagged with the
279 :code:`@logging_utils.squelched_logging_ok` decorator (see above);
280 all others are ignored.
282 This functionality is enabled by default but can be disabled via
283 the :code:`--no_logging_squelch_repeats` commandline flag.
286 def __init__(self) -> None:
288 self.counters: collections.Counter = collections.Counter()
291 def filter(self, record: logging.LogRecord) -> bool:
292 """Should we drop this log message?"""
293 id1 = f"{record.module}:{record.funcName}"
294 if id1 not in squelched_logging_counts:
296 threshold = squelched_logging_counts[id1]
297 logsite = f"{record.pathname}+{record.lineno}+{record.levelno}+{record.msg}"
298 count = self.counters[logsite]
299 self.counters[logsite] += 1
300 return count < threshold
303 # A map from function_identifier -> probability of logging (0.0%..100.0%)
304 probabilistic_logging_levels: Dict[str, float] = {}
307 def logging_is_probabilistic(probability_of_logging: float) -> Callable:
308 """A decorator that indicates that all logging statements within the
309 scope of a particular (marked via decorator) function are not
310 deterministic (i.e. they do not always unconditionally log) but rather
311 are probabilistic (i.e. they log N% of the time, randomly) when the
312 user passes the :code:`--logging_probabilistically` commandline flag
313 (which is enabled by default).
317 This affects *ALL* logging statements within the marked function.
318 If you want it to only affect a subset of logging statements,
319 log those statements in a separate function that you invoke
320 from within the "too large" scope and mark that separate function
321 with the :code:`logging_is_probabilistic` decorator instead.
323 That this functionality can be disabled (forcing all logged
324 messages to produce output) via the
325 :code:`--no_logging_probabilistically` cmdline argument.
328 def probabilistic_logging_wrapper(f: Callable):
329 from pyutils import function_utils
331 identifier = function_utils.function_identifier(f)
332 probabilistic_logging_levels[identifier] = probability_of_logging
335 return probabilistic_logging_wrapper
338 class ProbabilisticFilter(logging.Filter):
340 A filter that logs messages probabilistically (i.e. randomly at some
341 percent chance). This filter is used with a decorator (see
342 :meth:`logging_is_probabilistic`) to implement the
343 :code:`--logging_probabilistically` commandline flag.
345 This filter only affects logging messages from functions that have
346 been tagged with the `@logging_utils.probabilistic_logging` decorator.
350 def filter(self, record: logging.LogRecord) -> bool:
351 """Should the message be logged?"""
352 identifier = f"{record.module}:{record.funcName}"
353 threshold = probabilistic_logging_levels.get(identifier, 100.0)
354 return misc_utils.execute_probabilistically(threshold)
357 class OnlyInfoFilter(logging.Filter):
358 """A filter that only logs messages produced at the INFO logging
359 level. This is used by the :code:`--logging_info_is_print`
360 commandline option to select a subset of the logging stream to
361 send to a stdout handler.
365 def filter(self, record: logging.LogRecord):
366 return record.levelno == logging.INFO
369 class PrefixAddingFilter(logging.Filter):
370 """A filter that adds a string prefix to the log record for the
371 formatter to later fill in. Requires a %(prefix)s in the format
375 def __init__(self, prefix: str, klobber: bool = False):
381 self.klobber = klobber
384 def filter(self, record: logging.LogRecord):
386 record.prefix = self.prefix
387 elif "prefix" not in record.__dict__:
388 record.prefix = self.prefix
392 class SuffixAddingFilter(logging.Filter):
393 """A filter that adds a string suffix to the log record for the
394 formatter to later fill in. Requires a %(suffix)s in the format
398 def __init__(self, suffix: str, klobber: bool = False):
404 self.klobber = klobber
407 def filter(self, record: logging.LogRecord):
409 record.suffix = self.suffix
410 elif "suffix" not in record.__dict__:
411 record.suffix = self.suffix
415 class PrependingLogAdapter(logging.LoggerAdapter):
416 """:class:`LoggingContext` adds prefixes and suffixes using a
417 logging.Filter that must insert "prefix" or "suffix" members into
418 each log record by using :class:`PrefixAddingFilter` and
419 :class:`SuffixAddingFilter`. This relies on the logging format
420 string containing a %(prefix)s and a %(suffix)s to work correctly.
422 This is an alternate approach that basically just wraps the logger
423 in a class that has the same interface and thunks most calls down
424 to the wrapped logger. It might be useful if you don't want to use
425 :class:`LoggingContext` or its friends.
427 >>> logger = logging.getLogger(__name__ + ".PrependingLogAdapter")
428 >>> logger.setLevel(logging.INFO)
429 >>> logger.addHandler(logging.StreamHandler(sys.stdout))
431 At this point logger doesn't have any format string and so it is
432 missing %(prefix)s and %(suffix)s. It also doesn't have a
433 :class:`PrefixAddingFilter` or :class:`SuffixAddingFilter` added.
434 So using it in a :class:`LoggingContext` will not work.
436 But we can still add a prefix or suffix by just wrapping it:
438 >>> logger.info("TEST")
441 >>> log = PrependingLogAdapter.wrap_logger('prefix> ', logger)
446 def process(self, msg, kwargs):
447 return f'{self.extra.get("prefix", "")}{msg}', kwargs
450 def wrap_logger(prefix: str, logger: logging.Logger) -> logging.LoggerAdapter:
451 """Helper method around the creation of a LogAdapter that prepends
452 a given string to every log message produced.
455 prefix: the message to prepend to every log message.
456 logger: the logger whose messages to modify.
459 A new logger wrapping the old one with the given behavior.
460 The old logger will continue to behave as usual; simply drop
461 the reference to this wrapper when it's no longer needed.
464 return PrependingLogAdapter(logger, {"prefix": prefix})
467 class AppendingLogAdapter(logging.LoggerAdapter):
468 """:class:`LoggingContext` adds prefixes and suffixes using a
469 logging.Filter that must insert "prefix" or "suffix" members into
470 each log record by using :class:`PrefixAddingFilter` and
471 :class:`SuffixAddingFilter`. This relies on the logging format
472 string containing a %(prefix)s and a %(suffix)s to work correctly.
474 This is an alternate approach that basically just wraps the logger
475 in a class that has the same interface and thunks most calls down
476 to the wrapped logger. It might be useful if you don't want to use
477 :class:`LoggingContext` or its friends.
479 >>> logger = logging.getLogger(__name__ + ".AppendingLogAdapter")
480 >>> logger.setLevel(logging.INFO)
481 >>> logger.addHandler(logging.StreamHandler(sys.stdout))
483 At this point logger doesn't have any format string and so it is
484 missing %(prefix)s and %(suffix)s. It also doesn't have a
485 :class:`PrefixAddingFilter` or :class:`SuffixAddingFilter` added.
486 So using it in a :class:`LoggingContext` will not work.
488 But we can still add a prefix or suffix by just wrapping it:
490 >>> logger.info("TEST")
493 >>> log = AppendingLogAdapter.wrap_logger('!!!', logger)
498 def process(self, msg, kwargs):
499 return f'{msg}{self.extra.get("suffix", "")}', kwargs
502 def wrap_logger(suffix: str, logger: logging.Logger) -> logging.LoggerAdapter:
503 """Helper method around the creation of a LoggerAdapter that appends
504 a given string to every log message produced.
507 suffix: the message to prepend to every log message.
508 logger: the logger whose messages to modify.
511 A new logger wrapping the old one with the given behavior.
512 The old logger will continue to behave as usual; simply drop
513 the reference to this wrapper when it's no longer needed.
516 return AppendingLogAdapter(logger, {"suffix": suffix})
519 class LoggingContext(contextlib.ContextDecorator):
522 logger: logging.Logger,
524 level: Optional[int] = None,
525 handlers: Optional[List[logging.Handler]] = None,
526 prefix: Optional[str] = None,
527 suffix: Optional[str] = None,
529 """This is a logging context that can be used to temporarily change the
530 way we are logging within its scope. Logging changes may include:
532 - Changing the logging level (e.g. from INFO to DEBUG)
533 - Adding a prefix or suffix to every log message produced
534 - Adding temporary Handlers to direct the logging output elsewhere
536 Setup for doctest / examples. This will normally be taken care of
537 by code in :meth:`initialize_logging` so you don't have to worry
540 >>> logging_format = "%(prefix)s%(message)s%(suffix)s"
541 >>> logger = logging.getLogger(__name__ + ".LoggingContext")
542 >>> logger.setLevel(logging.INFO)
543 >>> handler = logging.StreamHandler(sys.stdout)
544 >>> handler.setFormatter(
545 ... MillisecondAwareFormatter(
546 ... fmt=logging_format,
550 >>> logger.addHandler(handler)
551 >>> logger.addFilter(PrefixAddingFilter(None))
552 >>> logger.addFilter(SuffixAddingFilter(None))
554 First, this logger should be currently be configured to send
555 INFO+ messages to sys.stdout. Let's see it in action:
557 >>> logger.info("Hello world!")
559 >>> logger.debug("You should not see this")
561 The first example is to simply change the level of the logger.
562 Here we temporarily change it to DEBUG within the body of the
563 :class:`LoggingContext`:
565 >>> with LoggingContext(logger, level=logging.DEBUG):
566 ... logger.debug("You should see this now")
567 ... logger.info("Of course you should still see this too")
568 You should see this now
569 Of course you should still see this too
571 >>> logger.debug("Outside of the context we are at INFO again")
572 >>> logger.debug("(which is why you don't see these)")
573 >>> logger.info("But you should see this at INFO level")
574 But you should see this at INFO level
576 The prefix and suffix argument prepend or append a message to
577 all log output. To do this, you need %(prefix)s and
578 %(suffix)s placeholders in your logger format string
579 indicating where to insert the data. This is useful, for
580 example, to add an active request identifier to the set of log
581 messages produced while processing it.
583 >>> logger.info("About to work on a new request")
584 About to work on a new request
586 >>> with LoggingContext(logger, prefix='10.0.0.13> '):
587 ... logger.info("Working on it now")
588 10.0.0.13> Working on it now
590 logger.info("Done with that request")
591 Done with that request
593 LoggingContext can also be used to add temporary handler(s).
594 This code temporarily uses two stdout handlers to double the
595 output for testing purporses but you could also temporarily,
596 e.g., add a RotatingFileHandler or SysLogHandler etc...
598 >>> with LoggingContext(logger, handlers=[logging.StreamHandler(sys.stdout)]):
599 ... logger.info("TEST")
603 Once leaving the context, logger's behavior is restored. In
604 this case, the extra handler is removed so output will not
607 >>> logger.info("OUTSIDE")
610 LoggingContext can also be used as a decorator if that is more
613 >>> @LoggingContext(logger, level=logging.DEBUG)
614 ... def log_stuff(logger):
615 ... logger.debug("But inside, the decorator has changed us to DEBUG")
617 >>> logger.debug("Outside, we're at INFO level and you don't see this")
618 >>> log_stuff(logger)
619 But inside, the decorator has changed us to DEBUG
620 >>> logger.debug("And, of course, out here we're still at INFO afterwards")
625 self.old_level: Optional[int] = None
626 self.handlers = handlers
628 self.prefix: Optional[logging.Filter] = PrefixAddingFilter(prefix, True)
632 self.suffix: Optional[logging.Filter] = SuffixAddingFilter(suffix, True)
636 def __enter__(self) -> Union[logging.Logger, logging.LoggerAdapter]:
639 self.old_level = self.logger.level
640 self.logger.setLevel(self.level)
643 for handler in self.handlers:
644 self.logger.addHandler(handler)
647 self.logger.addFilter(self.prefix)
650 self.logger.addFilter(self.suffix)
653 def __exit__(self, et, ev, tb) -> None:
655 self.logger.removeFilter(self.suffix)
658 self.logger.removeFilter(self.prefix)
661 for handler in self.handlers:
662 self.logger.removeHandler(handler)
666 self.logger.setLevel(self.old_level)
667 return None # propagate exceptions out
670 class MillisecondAwareFormatter(logging.Formatter):
671 """A formatter for adding milliseconds to log messages which, for
672 whatever reason, the default Python logger doesn't do.
676 You probably don't need to use this directly but it is
677 wired in under :meth:`initialize_logging` so that the
678 timestamps in log messages have millisecond level
682 converter = datetime.datetime.fromtimestamp # type: ignore
685 def formatTime(self, record, datefmt=None):
686 ct = MillisecondAwareFormatter.converter(
687 record.created, pytz.timezone("US/Pacific")
690 s = ct.strftime(datefmt)
692 t = ct.strftime("%Y-%m-%d %H:%M:%S")
693 s = f"{t},{record.msecs:%03d}"
697 def _log_about_logging(
699 default_logging_level,
700 preexisting_handlers_count,
704 """This is invoked automatically after logging is initialized such
705 that the first messages in the log are about how logging itself
708 level_name = logging._levelToName.get(
709 default_logging_level, str(default_logging_level)
711 logger.debug("Initialized global logging; logging level is %s.", level_name)
713 config.config["logging_clear_preexisting_handlers"]
714 and preexisting_handlers_count > 0
717 "Logging cleared %d global handlers (--logging_clear_preexisting_handlers)",
718 preexisting_handlers_count,
720 logger.debug('Logging format specification is "%s"', fmt)
721 if config.config["logging_debug_threads"]:
723 "...Logging format spec captures tid/pid. (--logging_debug_threads)"
725 if config.config["logging_debug_modules"]:
727 "...Logging format spec captures files/functions/lineno. (--logging_debug_modules)"
729 if config.config["logging_syslog"]:
731 "Logging to syslog as %s with priority mapping based on level. (--logging_syslog)",
734 if config.config["logging_filename"]:
736 'Logging to file "%s". (--logging_filename)',
737 config.config["logging_filename"],
740 "...with %d bytes max file size. (--logging_filename_maxsize)",
741 config.config["logging_filename_maxsize"],
744 "...and %d rotating backup file count. (--logging_filename_count)",
745 config.config["logging_filename_count"],
747 if config.config["logging_console"]:
748 logger.debug("Logging to the console (stderr). (--logging_console)")
749 if config.config["logging_info_is_print"]:
751 "Logging logger.info messages will be repeated on stdout. (--logging_info_is_print)"
753 if config.config["logging_squelch_repeats"]:
755 "Logging code allowed to request repeated messages be squelched. (--logging_squelch_repeats)"
759 "Logging code forbidden to request messages be squelched; all messages logged. (--no_logging_squelch_repeats)"
761 if config.config["logging_probabilistically"]:
763 "Logging code is allowed to request probabilistic logging. (--logging_probabilistically)"
767 "Logging code is forbidden to request probabilistic logging; messages always logged. (--no_logging_probabilistically)"
769 if config.config["logging_captures_prints"]:
771 "Logging will capture printed data as logger.info messages. (--logging_captures_prints)"
775 def _construct_logging_format() -> str:
776 """Figure out the root logging format string based on commandling args."""
778 # Custom or default --logging_format?
779 if config.config["logging_format"]:
780 fmt = config.config["logging_format"]
781 elif config.config["logging_syslog"]:
782 fmt = "%(levelname).1s:%(filename)s[%(process)d]: "
784 fmt = "%(levelname).1s:%(asctime)s: "
786 fmt += "%(prefix)s%(message)s%(suffix)s"
788 # --logging_debug_threads and --logging_debug_modules both affect
789 # the format by prepending information about the pid/tid or
791 if config.config["logging_debug_threads"]:
792 fmt = f"%(process)d.%(thread)d|{fmt}"
793 if config.config["logging_debug_modules"]:
794 fmt = f"%(filename)s:%(funcName)s:%(lineno)s|{fmt}"
798 def _construct_handlers_list(
799 logging_format: str, facility_name: Optional[str]
800 ) -> List[logging.Handler]:
801 """Create the list of handlers to be added to the root logger
802 based on commandline flags.
805 retval: List[logging.Handler] = []
807 # --logging_syslog (optionally with --logging_syslog_facility)
808 # sets up for logging to use the standard system syslogd as a
810 if config.config["logging_syslog"]:
811 if sys.platform not in ("win32", "cygwin"):
813 facility = SysLogHandler.__dict__.get(facility_name, SysLogHandler.LOG_USER)
815 syslog_handler = SysLogHandler(facility=facility, address="/dev/log")
816 syslog_handler.setFormatter(
817 MillisecondAwareFormatter(
819 datefmt=config.config["logging_date_format"],
822 retval.append(syslog_handler)
824 # --logging_filename (with friends --logging_filename_count and
825 # --logging_filename_maxsize) set up logging to a file on the
826 # filesystem with automatic rotation when it gets too big.
827 if config.config["logging_filename"]:
828 max_bytes = config.config["logging_filename_maxsize"]
829 assert max_bytes and isinstance(max_bytes, int)
830 backup_count = config.config["logging_filename_count"]
831 assert backup_count and isinstance(backup_count, int)
832 file_handler = RotatingFileHandler(
833 config.config["logging_filename"],
835 backupCount=backup_count,
837 file_handler.setFormatter(
838 MillisecondAwareFormatter(
840 datefmt=config.config["logging_date_format"],
843 retval.append(file_handler)
845 # --logging_console is, ahem, logging to the console.
846 if config.config["logging_console"]:
847 console_handler = logging.StreamHandler(sys.stderr)
848 console_handler.setFormatter(
849 MillisecondAwareFormatter(
851 datefmt=config.config["logging_date_format"],
854 retval.append(console_handler)
856 # --logging_info_is_print echoes any message to logger.info(x) as
857 # a print statement on stdout.
858 if config.config["logging_info_is_print"]:
859 print_handler = logging.StreamHandler(sys.stdout)
860 print_handler.addFilter(OnlyInfoFilter())
861 retval.append(print_handler)
864 retval.append(logging.NullHandler())
868 def _add_logging_handlers_and_filters(
869 logger: logging.Logger, handlers: List[logging.Handler]
871 """Adds a list of handlers to the root logger."""
873 for handler in handlers:
874 # These two are always present and set %(prefix) and %(suffix)
875 # if appropriate (see :class:`LoggingContext`) or make them
877 handler.addFilter(PrefixAddingFilter("", False))
878 handler.addFilter(SuffixAddingFilter("", False))
880 # --logging_squelch_repeats allows code to request repeat logging
881 # messages (identical log site and message contents) to be
882 # silenced. Logging code must request this explicitly, it isn't
883 # automatic. This option just allows the silencing to happen.
884 if config.config["logging_squelch_repeats"]:
885 handler.addFilter(SquelchRepeatedMessagesFilter())
887 # --logging_probabilistically allows code to request
888 # non-deterministic logging where messages have some probability
889 # of being produced. Logging code must request this explicitly.
890 # This option just allows the non-deterministic behavior to
891 # happen. Disabling it will cause every log message to be
893 if config.config["logging_probabilistically"]:
894 handler.addFilter(ProbabilisticFilter())
895 logger.addHandler(handler)
898 def initialize_logging(logger=None) -> logging.Logger:
899 """Initialize logging for the program. See module level comments
900 for information about what functionality this provides and how to
901 enable or disable functionality via the commandline.
904 :meth:`bootstrap.initialize` decorator on your program's entry point,
905 it will call this for you. See :meth:`pyutils.bootstrap.initialize`
909 global LOGGING_INITIALIZED
910 if LOGGING_INITIALIZED:
911 return logging.getLogger()
912 LOGGING_INITIALIZED = True
914 preexisting_handlers_count = 0
916 # --logging_clear_preexisting_handlers removes logging handlers
917 # that were registered by global statements during imported module
919 if config.config["logging_clear_preexisting_handlers"]:
920 logging.basicConfig(force=True)
921 logger = logging.getLogger()
922 while logger.hasHandlers():
923 logger.removeHandler(logger.handlers[0])
924 preexisting_handlers_count += 1
926 logging.basicConfig()
927 logger = logging.getLogger()
929 # --logging_config_file pulls logging settings from a config file
930 # skipping the rest of this setup.
931 if config.config["logging_config_file"] is not None:
932 fileConfig(config.config["logging_config_file"])
935 # Global default logging level (--logging_level); messages below
936 # this level will be silenced.
937 logging_level = config.config["logging_level"]
939 logging_level = logging_level.upper()
940 default_logging_level = getattr(logging, logging_level, None)
941 if not isinstance(default_logging_level, int):
942 raise ValueError(f'Invalid level: {config.config["logging_level"]}')
943 logger.setLevel(default_logging_level)
945 logging_format = _construct_logging_format()
947 if config.config["logging_syslog"]:
948 if sys.platform not in ("win32", "cygwin"):
949 if config.config["logging_syslog_facility"]:
950 facility_name = "LOG_" + config.config["logging_syslog_facility"]
951 handlers: List[logging.Handler] = _construct_handlers_list(
952 logging_format, facility_name
954 _add_logging_handlers_and_filters(logger, handlers)
956 # --logging_captures_prints, if set, will capture and log.info
957 # anything printed on stdout.
958 if config.config["logging_captures_prints"]:
961 def print_and_also_log(*arg, **kwarg):
962 f = kwarg.get("file", None)
967 BUILT_IN_PRINT(*arg, **kwarg)
969 builtins.print = print_and_also_log
971 # At this point the logger is ready, handlers are set up,
972 # etc... so log about the logging configuration.
975 default_logging_level,
976 preexisting_handlers_count,
983 def get_logger(name: str = ""):
984 """Get the global logger"""
985 logger = logging.getLogger(name)
986 return initialize_logging(logger)
989 def tprint(*args, **kwargs) -> None:
990 """Legacy function for printing a message augmented with thread id
991 still needed by some code. Please use --logging_debug_threads in
994 if config.config["logging_debug_threads"]:
995 from pyutils.parallelize.thread_utils import current_thread_id
997 print(f"{current_thread_id()}", end="")
998 print(*args, **kwargs)
1003 class OutputMultiplexer(object):
1004 """A class that broadcasts printed messages to several sinks
1005 (including various logging levels, different files, different file
1006 handles, the house log, etc...). See also
1007 :class:`OutputMultiplexerContext` for an easy usage pattern.
1010 class Destination(enum.IntEnum):
1011 """Bits in the destination_bitv bitvector. Used to indicate the
1012 output destination."""
1015 LOG_DEBUG = 0x01 # ⎫
1017 LOG_WARNING = 0x04 # ⎬ Must provide logger to the c'tor.
1018 LOG_ERROR = 0x08 # ⎪
1019 LOG_CRITICAL = 0x10 # ⎭
1020 FILENAMES = 0x20 # Must provide a filename to the c'tor.
1021 FILEHANDLES = 0x40 # Must provide a handle to the c'tor.
1023 ALL_LOG_DESTINATIONS = (
1024 LOG_DEBUG | LOG_INFO | LOG_WARNING | LOG_ERROR | LOG_CRITICAL
1026 ALL_OUTPUT_DESTINATIONS = 0x8F
1031 destination_bitv: int,
1034 filenames: Optional[Iterable[str]] = None,
1035 handles: Optional[Iterable[io.TextIOWrapper]] = None,
1038 Constructs the OutputMultiplexer instance.
1041 destination_bitv: a bitvector where each bit represents an
1042 output destination. Multiple bits may be set.
1043 logger: if LOG_* bits are set, you must pass a logger here.
1044 filenames: if FILENAMES bit is set, this should be a list of
1045 files you'd like to output into. This code handles opening
1046 and closing said files.
1047 handles: if FILEHANDLES bit is set, this should be a list of
1048 already opened filehandles you'd like to output into. The
1049 handles will remain open after the scope of the multiplexer.
1052 logger = logging.getLogger(None)
1053 self.logger = logger
1055 self.f: Optional[List[Any]] = None
1056 if filenames is not None:
1057 self.f = [open(filename, "wb", buffering=0) for filename in filenames]
1059 if destination_bitv & OutputMultiplexer.Destination.FILENAMES:
1060 raise ValueError("Filenames argument is required if bitv & FILENAMES")
1063 self.h: Optional[List[Any]] = None
1064 if handles is not None:
1065 self.h = list(handles)
1067 if destination_bitv & OutputMultiplexer.Destination.FILEHANDLES:
1068 raise ValueError("Handle argument is required if bitv & FILEHANDLES")
1071 self.set_destination_bitv(destination_bitv)
1073 def get_destination_bitv(self):
1074 """Where are we outputting?"""
1075 return self.destination_bitv
1077 def set_destination_bitv(self, destination_bitv: int):
1078 """Change the output destination_bitv to the one provided."""
1079 if destination_bitv & self.Destination.FILENAMES and self.f is None:
1080 raise ValueError("Filename argument is required if bitv & FILENAMES")
1081 if destination_bitv & self.Destination.FILEHANDLES and self.h is None:
1082 raise ValueError("Handle argument is required if bitv & FILEHANDLES")
1083 self.destination_bitv = destination_bitv
1085 def print(self, *args, **kwargs):
1086 """Produce some output to all sinks."""
1087 from pyutils.string_utils import _sprintf, strip_escape_sequences
1089 end = kwargs.pop("end", None)
1091 if not isinstance(end, str):
1092 raise TypeError("end must be None or a string")
1093 sep = kwargs.pop("sep", None)
1095 if not isinstance(sep, str):
1096 raise TypeError("sep must be None or a string")
1098 raise TypeError("invalid keyword arguments to print()")
1099 buf = _sprintf(*args, end="", sep=sep)
1106 if self.destination_bitv & self.Destination.FILENAMES and self.f is not None:
1108 _.write(buf.encode("utf-8"))
1111 if self.destination_bitv & self.Destination.FILEHANDLES and self.h is not None:
1116 buf = strip_escape_sequences(buf)
1117 if self.logger is not None:
1118 if self.destination_bitv & self.Destination.LOG_DEBUG:
1119 self.logger.debug(buf)
1120 if self.destination_bitv & self.Destination.LOG_INFO:
1121 self.logger.info(buf)
1122 if self.destination_bitv & self.Destination.LOG_WARNING:
1123 self.logger.warning(buf)
1124 if self.destination_bitv & self.Destination.LOG_ERROR:
1125 self.logger.error(buf)
1126 if self.destination_bitv & self.Destination.LOG_CRITICAL:
1127 self.logger.critical(buf)
1128 if self.destination_bitv & self.Destination.HLOG:
1132 """Close all open files."""
1133 if self.f is not None:
1138 class OutputMultiplexerContext(OutputMultiplexer, contextlib.ContextDecorator):
1140 A context that uses an :class:`OutputMultiplexer`. e.g.::
1142 with OutputMultiplexerContext(
1143 OutputMultiplexer.LOG_INFO |
1144 OutputMultiplexer.LOG_DEBUG |
1145 OutputMultiplexer.FILENAMES |
1146 OutputMultiplexer.FILEHANDLES,
1147 filenames = [ '/tmp/foo.log', '/var/log/bar.log' ],
1150 mplex.print("This is a log message!")
1155 destination_bitv: OutputMultiplexer.Destination,
1163 destination_bitv: a bitvector that indicates where we should
1164 send output. See :class:`OutputMultiplexer` for options.
1165 logger: optional logger to use for log destination messages.
1166 filenames: optional filenames to write for filename destination
1168 handles: optional open filehandles to write for filehandle
1169 destination messages.
1174 filenames=filenames,
1178 def __enter__(self):
1181 def __exit__(self, etype, value, traceback) -> bool:
1183 if etype is not None:
1188 def hlog(message: str) -> None:
1189 """Write a message to the house log (syslog facility local7 priority
1190 info) by calling `/usr/bin/logger`. This is pretty hacky but used
1191 by a bunch of (my) code. Another way to do this would be to use
1192 :code:`--logging_syslog` and :code:`--logging_syslog_facility` but
1193 I can't actually say that's easier.
1195 TODO: this needs to move.
1197 message = message.replace("'", "'\"'\"'")
1198 os.system(f"/usr/bin/logger -p local7.info -- '{message}'")
1201 if __name__ == "__main__":