简体   繁体   中英

How to use logging library? (Implement logging in Python)

I would like to apply logging library in a class to get reports from the different steps in my code and use info , debug or error functions and keep it in a logging file. In addition I want to use multiprocessing in my code as well. But I could not quite figure out how it works and should be set up, plus I have used it in a code and defined it as following

import logging 

logging.basicConfig(filename='logfile.log',level=logging.DEBUG)

it halted the code and prevents to terminate the process. I am wondering how it should be used in a class and stopped and closed the log file?!! Any help would be appreciated...

You may go through Good logging practice in python for getting more idea of logging module and get more detailed info from Python document .

Below is a basic example on how to use logging module, in which I am knowingly raising an exception:

import logging
log = logging.getLogger("mylog")
log.setLevel(logging.DEBUG)

formatter = logging.Formatter(
    "%(asctime)s %(threadName)-11s %(levelname)-10s %(message)s")
# Alternative formatting available on python 3.2+:
# formatter = logging.Formatter(
#     "{asctime} {threadName:>11} {levelname} {message}", style='{')

# Log to file
filehandler = logging.FileHandler("debug.txt", "w")
filehandler.setLevel(logging.DEBUG)
filehandler.setFormatter(formatter)
log.addHandler(filehandler)

# Log to stdout too
streamhandler = logging.StreamHandler()
streamhandler.setLevel(logging.INFO)
streamhandler.setFormatter(formatter)
log.addHandler(streamhandler)

# Test it
log.debug("Some message")
log.error("An error!")
try:
    something()
except:
    log.exception("An exception occured!")

In your debug.txt , you will get output as:

2011-01-18 12:07:24,943  MainThread  DEBUG      Some message
2011-01-18 12:07:24,943  MainThread  ERROR      An error!
2011-01-18 12:07:24,943  MainThread  ERROR      An exception occured!
Traceback (most recent call last):
  File "./logtest.py", line 17, in 
    something()
NameError: name 'something' is not defined

For my own projects I developed logging tool that helps me a lot.
The repository allows to set a config file or to define your preferences in the code. My way of approaching in a new script is:
1. copy the file `MyLogger.py` in your root directory 2. in the files where you want to log something do: ``` // script.py from MyLogger import Logger

log = Logger( name ).logger

log.info('hello')

 The output of the logger will even contain the namefile (script.py). <br> I hope it helps! <br> The <a href="https://github.com/vignif/LogMe">code is available</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM