[{"data":1,"prerenderedAt":428},["ShallowReactive",2],{"i-lucide:sun-moon":3,"i-lucide:menu":8,"post-\u002Fblog\u002F2022-11-03-python-logging":10,"surround-\u002Fblog\u002F2022-11-03-python-logging":411,"i-lucide:arrow-left":422,"i-lucide:sparkles":424,"i-lucide:user":426},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":7},0,24,false,"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M12 2v2m2.837 12.385a6 6 0 1 1-7.223-7.222c.624-.147.97.66.715 1.248a4 4 0 0 0 5.26 5.259c.589-.255 1.396.09 1.248.715M16 12a4 4 0 0 0-4-4m7-3l-1.256 1.256M20 12h2\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":9},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M4 5h16M4 12h16M4 19h16\"\u002F>",{"id":11,"title":12,"authors":13,"body":16,"date":399,"description":22,"extension":400,"meta":401,"navigation":108,"path":404,"seo":405,"stem":406,"tags":407,"toc":108,"__hash__":410},"blog\u002Fblog\u002F2022-11-03-python-logging.md","Using Python logging",[14,15],"gpt","pfarmer",{"type":17,"value":18,"toc":391},"minimark",[19,23,42,45,50,53,70,73,77,80,158,178,181,189,197,201,204,274,277,281,284,315,322,326,329,349,352,356,359,376,379,384,387],[20,21,22],"p",{},"The logging module in Python provides a standardized way to record diagnostic and runtime information from your programs. The key benefits of logging are the ability to:",[24,25,26,30,33,36,39],"ul",{},[27,28,29],"li",{},"Log messages with different verbosity levels (debug, info, warning, error)",[27,31,32],{},"Flexibly configure how and where log messages are outputted",[27,34,35],{},"Format log message output",[27,37,38],{},"Filter log messages",[27,40,41],{},"Log exceptions and stack traces",[20,43,44],{},"In this article, we'll cover the basic and advanced features of the logging module.",[46,47,49],"h2",{"id":48},"logging-levels","Logging Levels",[20,51,52],{},"The logging module provides the following severity levels to indicate the importance of log messages:",[24,54,55,58,61,64,67],{},[27,56,57],{},"DEBUG - Debugging information, typically only enabled during development",[27,59,60],{},"INFO - General information about program operation",[27,62,63],{},"WARNING - Warning messages about possible issues",[27,65,66],{},"ERROR - Error messages when something has gone wrong",[27,68,69],{},"CRITICAL - Very severe error events",[20,71,72],{},"By default, the logging module captures all messages at WARNING and above.",[46,74,76],{"id":75},"basic-logging-configuration","Basic Logging Configuration",[20,78,79],{},"To start logging from Python:",[81,82,87],"pre",{"className":83,"code":84,"language":85,"meta":86,"style":86},"language-python shiki shiki-themes github-light github-dark-default","import logging\n\nlogger = logging.getLogger(__name__)\nlogger.setLevel(logging.INFO) \n\nlogger.info(\"Started\")\n","python","",[88,89,90,103,110,129,141,146],"code",{"__ignoreMap":86},[91,92,95,99],"span",{"class":93,"line":94},"line",1,[91,96,98],{"class":97},"sDgYj","import",[91,100,102],{"class":101},"szmH1"," logging\n",[91,104,106],{"class":93,"line":105},2,[91,107,109],{"emptyLinePlaceholder":108},true,"\n",[91,111,113,116,119,122,126],{"class":93,"line":112},3,[91,114,115],{"class":101},"logger ",[91,117,118],{"class":97},"=",[91,120,121],{"class":101}," logging.getLogger(",[91,123,125],{"class":124},"sPgVT","__name__",[91,127,128],{"class":101},")\n",[91,130,132,135,138],{"class":93,"line":131},4,[91,133,134],{"class":101},"logger.setLevel(logging.",[91,136,137],{"class":124},"INFO",[91,139,140],{"class":101},") \n",[91,142,144],{"class":93,"line":143},5,[91,145,109],{"emptyLinePlaceholder":108},[91,147,149,152,156],{"class":93,"line":148},6,[91,150,151],{"class":101},"logger.info(",[91,153,155],{"class":154},"sNbzA","\"Started\"",[91,157,128],{"class":101},[24,159,160,163,169,175],{},[27,161,162],{},"Import the logging module",[27,164,165,166],{},"Get a logger instance using ",[88,167,168],{},"getLogger()",[27,170,171,172],{},"Set the log level on the logger using ",[88,173,174],{},"setLevel()",[27,176,177],{},"Call log methods on the logger to generate log messages",[20,179,180],{},"This outputs a log message like:",[81,182,187],{"className":183,"code":185,"language":186},[184],"language-text","INFO:__main__:Started\n","text",[88,188,185],{"__ignoreMap":86},[20,190,191,192,196],{},"The log message contains the level (INFO), logger name (",[193,194,195],"strong",{},"main","), and message (Started).",[46,198,200],{"id":199},"log-message-formatting","Log Message Formatting",[20,202,203],{},"To configure how log messages are formatted:",[81,205,207],{"className":83,"code":206,"language":85,"meta":86,"style":86},"formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n\nhandler = logging.StreamHandler()\nhandler.setFormatter(formatter)\n\nlogger.addHandler(handler)\n",[88,208,209,246,250,260,265,269],{"__ignoreMap":86},[91,210,211,214,216,219,222,226,229,232,234,237,239,242,244],{"class":93,"line":94},[91,212,213],{"class":101},"formatter ",[91,215,118],{"class":97},[91,217,218],{"class":101}," logging.Formatter(",[91,220,221],{"class":154},"'",[91,223,225],{"class":224},"sw7DY","%(asctime)s",[91,227,228],{"class":154}," - ",[91,230,231],{"class":224},"%(name)s",[91,233,228],{"class":154},[91,235,236],{"class":224},"%(levelname)s",[91,238,228],{"class":154},[91,240,241],{"class":224},"%(message)s",[91,243,221],{"class":154},[91,245,128],{"class":101},[91,247,248],{"class":93,"line":105},[91,249,109],{"emptyLinePlaceholder":108},[91,251,252,255,257],{"class":93,"line":112},[91,253,254],{"class":101},"handler ",[91,256,118],{"class":97},[91,258,259],{"class":101}," logging.StreamHandler()\n",[91,261,262],{"class":93,"line":131},[91,263,264],{"class":101},"handler.setFormatter(formatter)\n",[91,266,267],{"class":93,"line":143},[91,268,109],{"emptyLinePlaceholder":108},[91,270,271],{"class":93,"line":148},[91,272,273],{"class":101},"logger.addHandler(handler)\n",[20,275,276],{},"This formats each message as: timestamp - logger_name - level - message",[46,278,280],{"id":279},"logging-to-a-file","Logging to a File",[20,282,283],{},"To log messages to a file instead of the console:",[81,285,287],{"className":83,"code":286,"language":85,"meta":86,"style":86},"handler = logging.FileHandler('app.log')\nhandler.setFormatter(formatter)\n\nlogger.addHandler(handler) \n",[88,288,289,303,307,311],{"__ignoreMap":86},[91,290,291,293,295,298,301],{"class":93,"line":94},[91,292,254],{"class":101},[91,294,118],{"class":97},[91,296,297],{"class":101}," logging.FileHandler(",[91,299,300],{"class":154},"'app.log'",[91,302,128],{"class":101},[91,304,305],{"class":93,"line":105},[91,306,264],{"class":101},[91,308,309],{"class":93,"line":112},[91,310,109],{"emptyLinePlaceholder":108},[91,312,313],{"class":93,"line":131},[91,314,273],{"class":101},[20,316,317,318,321],{},"This will write log messages to ",[88,319,320],{},"app.log",".",[46,323,325],{"id":324},"advanced-configuration","Advanced Configuration",[20,327,328],{},"Here are some common advanced configuration needs:",[24,330,331,334,337,340,343,346],{},[27,332,333],{},"Configure separate loggers for different modules\u002Fcomponents",[27,335,336],{},"Log to multiple destinations simultaneously",[27,338,339],{},"Set different log levels for different loggers",[27,341,342],{},"Custom logging handlers for syslog, HTTP, databases, etc",[27,344,345],{},"Handler filters to only capture certain log messages",[27,347,348],{},"Rotation of log files based on size",[20,350,351],{},"All this is achieved by working directly with the objects from the logging module - loggers, handlers, formatters, filters etc.",[46,353,355],{"id":354},"best-practices","Best Practices",[20,357,358],{},"Some key best practices for working with logging in Python:",[24,360,361,364,367,370,373],{},[27,362,363],{},"Configure logging as early as possible in app initialisation",[27,365,366],{},"Use INFO for overall operational logging and DEBUG for diagnostics",[27,368,369],{},"Always log exceptions to get full tracebacks",[27,371,372],{},"Use meaningful logger names based on application\u002Fmodule",[27,374,375],{},"Avoid logging confidential information",[20,377,378],{},"This helps ensure logging is setup properly and provides the most useful diagnostic information.",[380,381,383],"h1",{"id":382},"conclusion","Conclusion",[20,385,386],{},"Python's logging module provides a full-featured and flexible logging system. It takes some initial configuration, but once setup allows comprehensive logging and debugging with control over how much information is captured and where it is sent. Following the standard patterns and best practices will ensure you get the most benefit.",[388,389,390],"style",{},"html pre.shiki code .sDgYj, html code.shiki .sDgYj{--shiki-default:#D73A49;--shiki-dark:#FF7B72}html pre.shiki code .szmH1, html code.shiki .szmH1{--shiki-default:#24292E;--shiki-dark:#E6EDF3}html pre.shiki code .sPgVT, html code.shiki .sPgVT{--shiki-default:#005CC5;--shiki-dark:#79C0FF}html pre.shiki code .sNbzA, html code.shiki .sNbzA{--shiki-default:#032F62;--shiki-dark:#A5D6FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sw7DY, html code.shiki .sw7DY{--shiki-default:#005CC5;--shiki-dark:#FF7B72}",{"title":86,"searchDepth":105,"depth":105,"links":392},[393,394,395,396,397,398],{"id":48,"depth":105,"text":49},{"id":75,"depth":105,"text":76},{"id":199,"depth":105,"text":200},{"id":279,"depth":105,"text":280},{"id":324,"depth":105,"text":325},{"id":354,"depth":105,"text":355},"2022-11-03","md",{"type":402,"layout":403},"post","blog_page","\u002Fblog\u002F2022-11-03-python-logging",{"title":12,"description":22},"blog\u002F2022-11-03-python-logging",[408,85,409],"devops","programming","MmkmDkfm51BXO5akJZVTQ-nIrr5lZLXZn77wRD4Fqzg",[412,417],{"title":413,"path":414,"stem":415,"date":416,"children":-1},"Using Linux nftables","\u002Fblog\u002F2022-10-15-using-nft","blog\u002F2022-10-15-using-nft","2022-10-15",{"title":418,"path":419,"stem":420,"date":421,"children":-1},"Using Python requests","\u002Fblog\u002F2022-11-17-python-requests","blog\u002F2022-11-17-python-requests","2022-11-17",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":423},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"m12 19l-7-7l7-7m7 7H5\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":425},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M11.017 2.814a1 1 0 0 1 1.966 0l1.051 5.558a2 2 0 0 0 1.594 1.594l5.558 1.051a1 1 0 0 1 0 1.966l-5.558 1.051a2 2 0 0 0-1.594 1.594l-1.051 5.558a1 1 0 0 1-1.966 0l-1.051-5.558a2 2 0 0 0-1.594-1.594l-5.558-1.051a1 1 0 0 1 0-1.966l5.558-1.051a2 2 0 0 0 1.594-1.594zM20 2v4m2-2h-4\"\u002F>\u003Ccircle cx=\"4\" cy=\"20\" r=\"2\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":427},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2\"\u002F>\u003Ccircle cx=\"12\" cy=\"7\" r=\"4\"\u002F>\u003C\u002Fg>",1783616166933]