Isn’t it troublesome to set the log file in python? Use LoggerGenerator

D. Akagawa
1 min readJun 30, 2021

--

This is my first article. I’m a robot and AI engineer in Japan. I’ve been used Python as my main programming language for several years.

When I’m writing codes for a big project, I always spend time setting up log file output such as

  • Determining the format to use for output
  • set some parameters, for example log level

When setting up to log using the logging library, most people would code as follows?

import loggingfilename = "Hoge.log"
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
fmt = logging.Formatter("%(asctime)s - %(levelname)s (%(filename)s) : %(message)s")
# For standard output
sh = logging.StreamHandler(sys.stdout) sh.setLevel(logging.DEBUG)
sh.setFormatter(fmt)
log.addHandler(sh)
# For file
fh = logging.FileHandler(filename)
fh.setFormatter(fmt)
log.addHandler(fh)

I’ve thought that this is annoying. So, I decides to create library for reducing time to write code for logging.

I created LoggerGenerator Library. You can easily prepare log function and use it like logging. You can find the repo here.

To install LoggerGenerator, you can use pip like pip install LoggerGeneraotr . After install it, please write two lines code at lease like below.

from LoggerGenerator import logger_genlogger_gen(globals())

logger_gen is a instance of LoggerGenerator class. You can generate logging instance by calling logger_gen(globals()) . After that, logging instance which name is log is generated. Once log is generated, you can call it like below.

log.debug("debug")
log.info("info")
log.error("error")
...

Thank you for reading my first article. I’m sorry that my English is not good. The next article, I’ll explain details about LoggerGenerator soon.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

D. Akagawa
D. Akagawa

No responses yet