Skip to content

Commit

Permalink
add more to logging module
Browse files Browse the repository at this point in the history
  • Loading branch information
SkafteNicki committed Nov 20, 2023
1 parent a1d7c58 commit f683c7c
Show file tree
Hide file tree
Showing 8 changed files with 373 additions and 10 deletions.
40 changes: 40 additions & 0 deletions figures/diagrams/logging_level.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<mxfile host="Electron" modified="2023-11-19T15:36:23.935Z" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/22.1.2 Chrome/114.0.5735.289 Electron/25.9.4 Safari/537.36" etag="ST1Dkfw6ak3WmEYY1pef" version="22.1.2" type="device">
<diagram name="Page-1" id="xsbP6zB01bjw9WcZV5e6">
<mxGraphModel dx="1050" dy="717" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="AyrUUDjSuoB-XR5cWeQk-1" value="&lt;font style=&quot;font-size: 20px;&quot;&gt;CRITICAL&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#a20025;fontColor=#ffffff;strokeColor=#6F0000;gradientColor=none;sketch=1;curveFitting=1;jiggle=2;" vertex="1" parent="1">
<mxGeometry x="159" y="290" width="120" height="60" as="geometry" />
</mxCell>
<mxCell id="AyrUUDjSuoB-XR5cWeQk-2" value="&lt;font style=&quot;font-size: 20px;&quot;&gt;ERROR&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d80073;fontColor=#ffffff;strokeColor=#A50040;sketch=1;curveFitting=1;jiggle=2;" vertex="1" parent="1">
<mxGeometry x="299" y="290" width="120" height="60" as="geometry" />
</mxCell>
<mxCell id="AyrUUDjSuoB-XR5cWeQk-3" value="&lt;font style=&quot;font-size: 20px;&quot;&gt;WARNING&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#6a00ff;fontColor=#ffffff;strokeColor=#3700CC;sketch=1;curveFitting=1;jiggle=2;" vertex="1" parent="1">
<mxGeometry x="439" y="290" width="120" height="60" as="geometry" />
</mxCell>
<mxCell id="AyrUUDjSuoB-XR5cWeQk-4" value="&lt;font style=&quot;font-size: 20px;&quot;&gt;INFO&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#0050ef;fontColor=#ffffff;strokeColor=#001DBC;sketch=1;curveFitting=1;jiggle=2;" vertex="1" parent="1">
<mxGeometry x="579" y="290" width="120" height="60" as="geometry" />
</mxCell>
<mxCell id="AyrUUDjSuoB-XR5cWeQk-5" value="&lt;font style=&quot;font-size: 20px;&quot;&gt;DEBUG&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#60a917;fontColor=#ffffff;strokeColor=#2D7600;sketch=1;curveFitting=1;jiggle=2;" vertex="1" parent="1">
<mxGeometry x="719" y="290" width="120" height="60" as="geometry" />
</mxCell>
<mxCell id="AyrUUDjSuoB-XR5cWeQk-6" value="&lt;font style=&quot;font-size: 20px;&quot;&gt;OFF&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#1ba1e2;fontColor=#ffffff;strokeColor=#006EAF;sketch=1;curveFitting=1;jiggle=2;" vertex="1" parent="1">
<mxGeometry x="20" y="290" width="120" height="60" as="geometry" />
</mxCell>
<mxCell id="AyrUUDjSuoB-XR5cWeQk-7" value="" style="endArrow=classic;startArrow=classic;html=1;rounded=0;strokeWidth=3;" edge="1" parent="1">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="20" y="370" as="sourcePoint" />
<mxPoint x="840" y="370" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="AyrUUDjSuoB-XR5cWeQk-8" value="&lt;font style=&quot;font-size: 20px;&quot;&gt;Least Logging&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="30" y="380" width="130" height="30" as="geometry" />
</mxCell>
<mxCell id="AyrUUDjSuoB-XR5cWeQk-9" value="&lt;font style=&quot;font-size: 20px;&quot;&gt;Most Logging&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="699" y="380" width="130" height="30" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
File renamed without changes.
Binary file added figures/logging_level.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added figures/rich_terminal_logging.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added figures/wandb_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions s4_debugging_and_logging/exercise_files/custom_python_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import logging
import logging.config
import sys
import time
from pathlib import Path

from rich.logging import RichHandler

LOGS_DIR = Path("logs")
LOGS_DIR.mkdir(parents=True, exist_ok=True)

logging_config = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"minimal": {"format": "%(message)s"},
"detailed": {
"format": "%(levelname)s %(asctime)s [%(name)s:%(filename)s:%(funcName)s:%(lineno)d]\n%(message)s\n"
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"stream": sys.stdout,
"formatter": "minimal",
"level": logging.DEBUG,
},
"info": {
"class": "logging.handlers.RotatingFileHandler",
"filename": Path(LOGS_DIR, "info.log"),
"maxBytes": 10485760, # 1 MB
"backupCount": 10,
"formatter": "detailed",
"level": logging.INFO,
},
"error": {
"class": "logging.handlers.RotatingFileHandler",
"filename": Path(LOGS_DIR, "error.log"),
"maxBytes": 10485760, # 1 MB
"backupCount": 10,
"formatter": "detailed",
"level": logging.ERROR,
},
},
"root": {
"handlers": ["console", "info", "error"],
"level": logging.INFO,
"propagate": True,
},
}

logging.config.dictConfig(logging_config)
logger = logging.getLogger()
logger.handlers[0] = RichHandler(markup=True) # set rich handler

if __name__ == "__main__":
# Sample messages (note that we use configured `logger` now)
logger.debug("Used for debugging your code.")
logger.info("Informative messages from your code.")

time.sleep(5)

logger.warning("Everything works but there is something to be aware of.")
logger.error("There's been a mistake with the process.")
logger.critical("There is something terribly wrong and process may terminate.")
41 changes: 41 additions & 0 deletions s4_debugging_and_logging/exercise_files/logger_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
formatters:
keys: minimal,detailed

formatter_minimal:
format: "%(message)s"

formatter_detailed:
format:
"%(levelname)s %(asctime)s [%(name)s:%(filename)s:%(funcName)s:%(lineno)d]%(message)s"

handlers:
keys: "console,info,error"

handler_console:
class: StreamHandler
level: DEBUG
formatter: minimal
args: (sys.stdout,)

handler_info:
class: handlers.RotatingFileHandler
level: INFO
formatter: detailed
backupCount: 10
maxBytes: 10485760
args: ("logs/info.log",)

handler_error:
class: handlers.RotatingFileHandler
level: ERROR
formatter: detailed
backupCount: 10
maxBytes: 10485760
args: ("logs/error.log",)

loggers:
keys: root

logger_root:
level: INFO
handlers: console,info,error
Loading

0 comments on commit f683c7c

Please sign in to comment.