axnet
axnet

Reputation: 5780

How to configure logging with colour, format etc in separate setting file in python?

I am trying to call python script from bash script. (Note: I am using python version 3.7) Following is the Directory structure (so_test is a directory)

so_test
  shell_script_to_call_py.sh
  main_file.py
  log_settings.py

files are as below,

shell_script_to_call_py.sh

#!/bin/bash

echo "...Enable Debug..."
python $(cd $(dirname ${BASH_SOURCE[0]}) && pwd)/main_file.py "input_1" --debug

echo "...No Debug..."
python $(cd $(dirname ${BASH_SOURCE[0]}) && pwd)/main_file.py "input_2"

main_file.py

import argparse
import importlib

importlib.import_module("log_settings.py")
from so_test import log_settings


def func1():
    log.info("INFO Test")
    log.debug("DEBUG Test")
    log.warning("WARN Test")


def func2():
    log.info("INFO Test")
    log.debug("DEBUG Test")
    log.warning("WARN Test")


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("input", type=str, help="input argument 1 is missing")
    parser.add_argument("--debug", help="to print debug logs", action="store_true")
    args = parser.parse_args()
    log_settings.log_conf(args.debug)
    log.info("INFO Test")
    log.debug("DEBUG Test")
    log.warning("WARN Test")
    func1()
    func2()


if __name__ == "__main__":
    main()

log_settings.py

import logging
from colorlog import ColoredFormatter


def log_config(is_debug_level):
    log_format = "%(log_color)s %(levelname)s %(message)s"
    if is_debug_level:
        logging.root.setLevel(logging.DEBUG)
    else:
        logging.root.setLevel(logging.INFO)
    stream = logging.StreamHandler()
    stream.setFormatter(ColoredFormatter(log_format))
    global log
    log = logging.getLogger('pythonConfig')
    log.addHandler(stream)

Following are 2 issues I am facing. (as a newbie to python)

  1. I am not able to import the log_settings.py properly in main_file.py
  2. I want to access use log.debug, log.info etc. in main_file (and other .py file) across different functions, for which the settings (format, color etc.) is declared in log_settings.py file.

Upvotes: 3

Views: 191

Answers (1)

Gopinath
Gopinath

Reputation: 4937

I got the code working with the following changes:

  1. Declare 'log' variable outside the function in log_settings.py, so that it can be imported by other programs.

  2. Rename the function named log_config to log_conf, which is referred in the main program.

  3. In the main program, update the import statements to import 'log' and 'log_conf' from log_settings

Working code:

1. log_settings.py

import logging
from colorlog import ColoredFormatter

global log
log = logging.getLogger('pythonConfig')

def log_conf(is_debug_level):
    log_format = "%(log_color)s %(levelname)s %(message)s"
    if is_debug_level:
        logging.root.setLevel(logging.DEBUG)
    else:
        logging.root.setLevel(logging.INFO)
    stream = logging.StreamHandler()
    stream.setFormatter(ColoredFormatter(log_format))
    log.addHandler(stream)

2. main_file.py

import argparse
import importlib

from log_settings import log_conf, log


def func1():
    log.info("INFO Test")
    log.debug("DEBUG Test")
    log.warning("WARN Test")


def func2():
    log.info("INFO Test")
    log.debug("DEBUG Test")
    log.warning("WARN Test")


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("input", type=str, help="input argument 1 is missing")
    parser.add_argument("--debug", help="to print debug logs", action="store_true")
    args = parser.parse_args()
    log_conf(args.debug)
    log.info("INFO Test")
    log.debug("DEBUG Test")
    log.warning("WARN Test")
    func1()
    func2()


if __name__ == "__main__":
    main()

Testing

$ python3 main_file.py "input_1" --debug

INFO INFO Test (Shows in Green)

DEBUG DEBUG Test (Shows in White)

WARNING WARN Test (Shows in Yellow)

INFO INFO Test

DEBUG DEBUG Test

WARNING WARN Test

INFO INFO Test

DEBUG DEBUG Test

WARNING WARN Test

Upvotes: 1

Related Questions