3

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...

0

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.