<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Needing help with using python3's logging module (handler, logger) in Controllers</title>
    <link>https://community.cisco.com/t5/controllers/needing-help-with-using-python3-s-logging-module-handler-logger/m-p/4091911#M2035</link>
    <description>&lt;P&gt;Thank you omz!&amp;nbsp; That gave me a great start.&amp;nbsp; I not only was able to run it and but also expanded to more handlers to multiple files, the way I want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 26 May 2020 03:54:02 GMT</pubDate>
    <dc:creator>pn2020</dc:creator>
    <dc:date>2020-05-26T03:54:02Z</dc:date>
    <item>
      <title>Needing help with using python3's logging module (handler, logger)</title>
      <link>https://community.cisco.com/t5/controllers/needing-help-with-using-python3-s-logging-module-handler-logger/m-p/4088968#M2033</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I need help with using python 3's standard logging module. I can't figure it out. Any clue is appreciatd!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It broke at the logging.config.dictConfig(config) line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I run main_log.py, I got this error message:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;logging.config.dictConfig(config)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;File "C:\Python38\lib\logging\config.py", line 808, in dictConfig&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;dictConfigClass(config).configure()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;File "C:\Python38\lib\logging\config.py", line 570, in configure&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;raise ValueError('Unable to configure handler '&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ValueError: Unable to configure handler 'file'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;===========&lt;BR /&gt;main_log.py&lt;BR /&gt;===========&lt;BR /&gt;&lt;BR /&gt;import logging&lt;BR /&gt;import logging.config&lt;BR /&gt;import os&lt;BR /&gt;import yaml&lt;BR /&gt;&lt;BR /&gt;class SetupLogging:&lt;BR /&gt;def __init__(self):&lt;BR /&gt;self.default_config = os.path.join(os.path.dirname(&lt;BR /&gt;os.path.abspath('__file__')), "templates/logging.yaml")&lt;BR /&gt;&lt;BR /&gt;def setup_logging(self, default_level=logging.INFO, env_key='LOG_CFG'):&lt;BR /&gt;path = self.default_config&lt;BR /&gt;env_path = os.getenv(env_key, None)&lt;BR /&gt;if env_path:&lt;BR /&gt;path = env_path&lt;BR /&gt;if os.path.exists(path):&lt;BR /&gt;config = yaml.safe_load(open(path, 'rt'))&lt;BR /&gt;logging.config.dictConfig(config)&lt;BR /&gt;else:&lt;BR /&gt;print('Error in loading logging configuration. Using default configs')&lt;BR /&gt;logging.basicConfig(level=default_level)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;SetupLogging().setup_logging()&lt;BR /&gt;&lt;BR /&gt;logging.info('%s', 'test.')&lt;BR /&gt;&lt;BR /&gt;===========&lt;BR /&gt;logging.yaml&lt;BR /&gt;===========&lt;BR /&gt;&lt;BR /&gt;---&lt;BR /&gt;version: 1&lt;BR /&gt;disable_existing_loggers: False&lt;BR /&gt;formatters:&lt;BR /&gt;user:&lt;BR /&gt;format: "[%(asctime)s][%(levelname)s] %(message)s"&lt;BR /&gt;datefmt: "%H:%M:%S"&lt;BR /&gt;simple:&lt;BR /&gt;format: "[%(asctime)s][%(name)s][%(levelname)s] %(message)s"&lt;BR /&gt;datefmt: "%m/%d/%Y %H:%M:%S"&lt;BR /&gt;handlers:&lt;BR /&gt;console:&lt;BR /&gt;class: logging.StreamHandler&lt;BR /&gt;level: INFO&lt;BR /&gt;formatter: user&lt;BR /&gt;stream: ext://sys.stdout&lt;BR /&gt;file:&lt;BR /&gt;class: logging.handlers.RotatingFileHandler&lt;BR /&gt;level: INFO&lt;BR /&gt;formatter: simple&lt;BR /&gt;filename: /opt/abc/file.log&lt;BR /&gt;maxBytes: 10485760 # 10MB&lt;BR /&gt;backupCount: 20&lt;BR /&gt;encoding: utf8&lt;BR /&gt;loggers:&lt;BR /&gt;xyz_tool:&lt;BR /&gt;level: INFO&lt;BR /&gt;handlers: [console, file]&lt;BR /&gt;propagate: no&lt;BR /&gt;root:&lt;BR /&gt;level: INFO&lt;BR /&gt;handlers: [file, ]&lt;BR /&gt;...&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 23:25:39 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/needing-help-with-using-python3-s-logging-module-handler-logger/m-p/4088968#M2033</guid>
      <dc:creator>pn2020</dc:creator>
      <dc:date>2020-05-19T23:25:39Z</dc:date>
    </item>
    <item>
      <title>Re: Needing help with using python3's logging module (handler, logger)</title>
      <link>https://community.cisco.com/t5/controllers/needing-help-with-using-python3-s-logging-module-handler-logger/m-p/4090395#M2034</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;I have re-formatted yaml ..&amp;nbsp;&lt;A href="https://onlineyamltools.com/prettify-yaml" target="_blank" rel="noopener"&gt;https://onlineyamltools.com/prettify-yaml&lt;/A&gt;&lt;/P&gt;&lt;P&gt;just to make it look pretty &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Make sure&amp;nbsp;&lt;STRONG&gt;/opt/abc/ &lt;/STRONG&gt;directory exists to create the log file &lt;STRONG&gt;file.log.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;I used the Desktop for test.&lt;/P&gt;&lt;PRE&gt;version: 1
disable_existing_loggers: false
formatters:
  user:
    format: '[%(asctime)s][%(levelname)s] %(message)s'
    datefmt: '%H:%M:%S'
  simple:
    format: '[%(asctime)s][%(name)s][%(levelname)s] %(message)s'
    datefmt: '%m/%d/%Y %H:%M:%S'
handlers:
  console:
    class: logging.StreamHandler
    level: INFO
    formatter: user
    stream: 'ext://sys.stdout'
  file:
    class: logging.handlers.RotatingFileHandler
    level: INFO
    formatter: simple
    filename: &lt;STRONG&gt;/Users/omz/Desktop/file.log&lt;/STRONG&gt;
    maxBytes: 10485760
    backupCount: 20
    encoding: utf8
loggers:
  xyz_tool:
    level: INFO
    handlers:
      - console
      - file
    propagate: 'no'
root:
  level: INFO
  handlers:
    - file&lt;/PRE&gt;&lt;P&gt;Only changed the file open method in the python code.&lt;/P&gt;&lt;PRE&gt;import logging
import logging.config
import os
import yaml

class SetupLogging:
    def __init__(self):
        self.default_config = os.path.join(os.path.dirname(
            os.path.abspath('__file__')), "logging.yaml")

    def setup_logging(self, default_level=logging.INFO, env_key='LOG_CFG'):
        path = self.default_config
        env_path = os.getenv(env_key, None)
        if env_path:
            path = env_path
        if os.path.exists(path):
            &lt;STRONG&gt;with open(path, 'rt') as f:
                config = yaml.safe_load(f.read())
                logging.config.dictConfig(config)&lt;/STRONG&gt;
        else:
            print('Error in loading logging configuration. Using default configs')
            logging.basicConfig(level=default_level)

SetupLogging().setup_logging()

logging.info('%s', 'test.')&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="python-logging.gif" style="width: 991px;"&gt;&lt;img src="https://community.cisco.com/t5/image/serverpage/image-id/75181iB954063E46425BDE/image-size/large?v=v2&amp;amp;px=999" role="button" title="python-logging.gif" alt="python-logging.gif" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 22:48:36 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/needing-help-with-using-python3-s-logging-module-handler-logger/m-p/4090395#M2034</guid>
      <dc:creator>omz</dc:creator>
      <dc:date>2020-05-21T22:48:36Z</dc:date>
    </item>
    <item>
      <title>Re: Needing help with using python3's logging module (handler, logger)</title>
      <link>https://community.cisco.com/t5/controllers/needing-help-with-using-python3-s-logging-module-handler-logger/m-p/4091911#M2035</link>
      <description>&lt;P&gt;Thank you omz!&amp;nbsp; That gave me a great start.&amp;nbsp; I not only was able to run it and but also expanded to more handlers to multiple files, the way I want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 May 2020 03:54:02 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/needing-help-with-using-python3-s-logging-module-handler-logger/m-p/4091911#M2035</guid>
      <dc:creator>pn2020</dc:creator>
      <dc:date>2020-05-26T03:54:02Z</dc:date>
    </item>
  </channel>
</rss>

