cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
7476
Views
10
Helpful
2
Replies

Needing help with using python3's logging module (handler, logger)

pn2020
Level 1
Level 1

I need help with using python 3's standard logging module. I can't figure it out. Any clue is appreciatd!
It broke at the logging.config.dictConfig(config) line.


When I run main_log.py, I got this error message:

logging.config.dictConfig(config)
File "C:\Python38\lib\logging\config.py", line 808, in dictConfig
dictConfigClass(config).configure()
File "C:\Python38\lib\logging\config.py", line 570, in configure
raise ValueError('Unable to configure handler '
ValueError: Unable to configure handler 'file'

 

===========
main_log.py
===========

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__')), "templates/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):
config = yaml.safe_load(open(path, 'rt'))
logging.config.dictConfig(config)
else:
print('Error in loading logging configuration. Using default configs')
logging.basicConfig(level=default_level)


SetupLogging().setup_logging()

logging.info('%s', 'test.')

===========
logging.yaml
===========

---
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: /opt/abc/file.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
loggers:
xyz_tool:
level: INFO
handlers: [console, file]
propagate: no
root:
level: INFO
handlers: [file, ]
...

1 Accepted Solution

Accepted Solutions

omz
VIP Alumni
VIP Alumni

Hi

I have re-formatted yaml .. https://onlineyamltools.com/prettify-yaml

just to make it look pretty :)

Make sure /opt/abc/ directory exists to create the log file file.log.

I used the Desktop for test.

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: /Users/omz/Desktop/file.log
    maxBytes: 10485760
    backupCount: 20
    encoding: utf8
loggers:
  xyz_tool:
    level: INFO
    handlers:
      - console
      - file
    propagate: 'no'
root:
  level: INFO
  handlers:
    - file

Only changed the file open method in the python code.

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):
            with open(path, 'rt') as f:
                config = yaml.safe_load(f.read())
                logging.config.dictConfig(config)
        else:
            print('Error in loading logging configuration. Using default configs')
            logging.basicConfig(level=default_level)

SetupLogging().setup_logging()

logging.info('%s', 'test.')

python-logging.gif

View solution in original post

2 Replies 2

omz
VIP Alumni
VIP Alumni

Hi

I have re-formatted yaml .. https://onlineyamltools.com/prettify-yaml

just to make it look pretty :)

Make sure /opt/abc/ directory exists to create the log file file.log.

I used the Desktop for test.

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: /Users/omz/Desktop/file.log
    maxBytes: 10485760
    backupCount: 20
    encoding: utf8
loggers:
  xyz_tool:
    level: INFO
    handlers:
      - console
      - file
    propagate: 'no'
root:
  level: INFO
  handlers:
    - file

Only changed the file open method in the python code.

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):
            with open(path, 'rt') as f:
                config = yaml.safe_load(f.read())
                logging.config.dictConfig(config)
        else:
            print('Error in loading logging configuration. Using default configs')
            logging.basicConfig(level=default_level)

SetupLogging().setup_logging()

logging.info('%s', 'test.')

python-logging.gif

Thank you omz!  That gave me a great start.  I not only was able to run it and but also expanded to more handlers to multiple files, the way I want.

 

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: