From 0052c7d63c117339342bb1c5991dd47e3df2ee9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Mon, 19 Aug 2024 16:42:59 +0200 Subject: [PATCH] Logging --- README.md | 46 +++++++++++++++++++++++----------------------- index.html | 52 ++++++++++++++++++++++++++-------------------------- 2 files changed, 49 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 672a5838..05758107 100644 --- a/README.md +++ b/README.md @@ -2234,35 +2234,35 @@ match : Logging ------- ```python -import logging +import logging as log ``` ```python -logging.basicConfig(filename=, level='DEBUG') # Configures the root logger (see Setup). -logging.debug/info/warning/error/critical() # Logs to the root logger. - = logging.getLogger(__name__) # Logger named after the module. -.() # Logs to the logger. -.exception() # Error() that appends caught exception. +log.basicConfig(filename=, level='DEBUG') # Configures the root logger (see Setup). +log.debug/info/warning/error/critical() # Logs to the root logger. + = log.getLogger(__name__) # Logger named after the module. +.() # Logs to the logger. +.exception() # Error() that appends caught exception. ``` ### Setup ```python -logging.basicConfig( - filename=None, # Logs to stderr or appends to file. - format='%(levelname)s:%(name)s:%(message)s', # Add '%(asctime)s' for local datetime. - level=logging.WARNING, # Drops messages with lower priority. - handlers=[logging.StreamHandler(sys.stderr)] # Uses FileHandler if filename is set. +log.basicConfig( + filename=None, # Logs to stderr or appends to file. + format='%(levelname)s:%(name)s:%(message)s', # Add '%(asctime)s' for local datetime. + level=log.WARNING, # Drops messages with lower priority. + handlers=[log.StreamHandler(sys.stderr)] # Uses FileHandler if filename is set. ) ``` ```python - = logging.Formatter('') # Creates a Formatter. - = logging.FileHandler(, mode='a') # Creates a Handler. Also `encoding=None`. -.setFormatter() # Adds Formatter to the Handler. -.setLevel() # Processes all messages by default. -.addHandler() # Adds Handler to the Logger. -.setLevel() # What is sent to its/ancestors' handlers. -.propagate = # Cuts off ancestors' handlers if False. + = log.Formatter('') # Creates a Formatter. + = log.FileHandler(, mode='a') # Creates a Handler. Also `encoding=None`. +.setFormatter() # Adds Formatter to the Handler. +.setLevel() # Processes all messages by default. +.addHandler() # Adds Handler to the Logger. +.setLevel() # What is sent to its/ancestors' handlers. +.propagate = # Cuts off ancestors' handlers if False. ``` * **Parent logger can be specified by naming the child logger `'.'`.** * **If logger doesn't have a set level it inherits it from the first ancestor that does.** @@ -2271,13 +2271,13 @@ logging.basicConfig( #### Creates a logger that writes all messages to file and sends them to the root's handler that prints warnings or higher: ```python ->>> logger = logging.getLogger('my_module') ->>> handler = logging.FileHandler('test.log', encoding='utf-8') ->>> handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s:%(name)s:%(message)s')) +>>> logger = log.getLogger('my_module') +>>> handler = log.FileHandler('test.log', encoding='utf-8') +>>> handler.setFormatter(log.Formatter('%(asctime)s %(levelname)s:%(name)s:%(message)s')) >>> logger.addHandler(handler) >>> logger.setLevel('DEBUG') ->>> logging.basicConfig() ->>> logging.root.handlers[0].setLevel('WARNING') +>>> log.basicConfig() +>>> log.root.handlers[0].setLevel('WARNING') >>> logger.critical('Running out of disk space.') CRITICAL:my_module:Running out of disk space. >>> print(open('test.log').read()) diff --git a/index.html b/index.html index 14268568..7b84ecf7 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -1834,30 +1834,30 @@

Format

'README.md is a readme file that belongs to user gto.'

-

#Logging

import logging
+

#Logging

import logging as log
 
-
logging.basicConfig(filename=<path>, level='DEBUG')  # Configures the root logger (see Setup).
-logging.debug/info/warning/error/critical(<str>)     # Logs to the root logger.
-<Logger> = logging.getLogger(__name__)               # Logger named after the module.
-<Logger>.<level>(<str>)                              # Logs to the logger.
-<Logger>.exception(<str>)                            # Error() that appends caught exception.
+
log.basicConfig(filename=<path>, level='DEBUG')   # Configures the root logger (see Setup).
+log.debug/info/warning/error/critical(<str>)      # Logs to the root logger.
+<Logger> = log.getLogger(__name__)                # Logger named after the module.
+<Logger>.<level>(<str>)                           # Logs to the logger.
+<Logger>.exception(<str>)                         # Error() that appends caught exception.
 
-

Setup

logging.basicConfig(
-    filename=None,                                   # Logs to stderr or appends to file.
-    format='%(levelname)s:%(name)s:%(message)s',     # Add '%(asctime)s' for local datetime.
-    level=logging.WARNING,                           # Drops messages with lower priority.
-    handlers=[logging.StreamHandler(sys.stderr)]     # Uses FileHandler if filename is set.
+

Setup

log.basicConfig(
+    filename=None,                                # Logs to stderr or appends to file.
+    format='%(levelname)s:%(name)s:%(message)s',  # Add '%(asctime)s' for local datetime.
+    level=log.WARNING,                            # Drops messages with lower priority.
+    handlers=[log.StreamHandler(sys.stderr)]      # Uses FileHandler if filename is set.
 )
 
-
<Formatter> = logging.Formatter('<format>')          # Creates a Formatter.
-<Handler> = logging.FileHandler(<path>, mode='a')    # Creates a Handler. Also `encoding=None`.
-<Handler>.setFormatter(<Formatter>)                  # Adds Formatter to the Handler.
-<Handler>.setLevel(<int/str>)                        # Processes all messages by default.
-<Logger>.addHandler(<Handler>)                       # Adds Handler to the Logger.
-<Logger>.setLevel(<int/str>)                         # What is sent to its/ancestors' handlers.
-<Logger>.propagate = <bool>                          # Cuts off ancestors' handlers if False.
+
<Formatter> = log.Formatter('<format>')           # Creates a Formatter.
+<Handler> = log.FileHandler(<path>, mode='a')     # Creates a Handler. Also `encoding=None`.
+<Handler>.setFormatter(<Formatter>)               # Adds Formatter to the Handler.
+<Handler>.setLevel(<int/str>)                     # Processes all messages by default.
+<Logger>.addHandler(<Handler>)                    # Adds Handler to the Logger.
+<Logger>.setLevel(<int/str>)                      # What is sent to its/ancestors' handlers.
+<Logger>.propagate = <bool>                       # Cuts off ancestors' handlers if False.
 
  • Parent logger can be specified by naming the child logger '<parent>.<name>'.
  • @@ -1865,17 +1865,17 @@

    Format

    'handlers.RotatingFileHandler' creates and deletes log files based on 'maxBytes' and 'backupCount' arguments.

-

Creates a logger that writes all messages to file and sends them to the root's handler that prints warnings or higher:

>>> logger = logging.getLogger('my_module')
->>> handler = logging.FileHandler('test.log', encoding='utf-8')
->>> handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s:%(name)s:%(message)s'))
+

Creates a logger that writes all messages to file and sends them to the root's handler that prints warnings or higher:

>>> logger = log.getLogger('my_module')
+>>> handler = log.FileHandler('test.log', encoding='utf-8')
+>>> handler.setFormatter(log.Formatter('%(asctime)s %(levelname)s:%(name)s:%(message)s'))
 >>> logger.addHandler(handler)
 >>> logger.setLevel('DEBUG')
->>> logging.basicConfig()
->>> logging.root.handlers[0].setLevel('WARNING')
+>>> log.basicConfig()
+>>> log.root.handlers[0].setLevel('WARNING')
 >>> logger.critical('Running out of disk space.')
 CRITICAL:my_module:Running out of disk space.
 >>> print(open('test.log').read())
-2023-02-07 23:21:01,430 CRITICAL:my_module:Running out of disk space.
+2023-02-07 23:21:01,430 CRITICAL:my_module:Running out of disk space.
 

#Introspection

<list> = dir()                          # Names of local vars, functions, classes and modules.
@@ -2932,7 +2932,7 @@ 

Format