Questions tagged [python-logging]

"logging" is a python module used for logging output to the console. Mainly used for debugging.

The logging module is a python module for logging output to the console. Most often it is used for debugging a program by printing output when specific conditions are met.

880 questions
896
votes
15 answers

python exception message capturing

import ftplib import urllib2 import os import logging logger = logging.getLogger('ftpuploader') hdlr = logging.FileHandler('ftplog.log') formatter = logging.Formatter('%(asctime)s %(levelname)s…
Hellnar
  • 62,315
  • 79
  • 204
  • 279
731
votes
17 answers

How do I log a Python error with debug information?

I am printing Python exception messages to a log file with logging.error: import logging try: 1/0 except ZeroDivisionError as e: logging.error(e) # ERROR:root:division by zero Is it possible to print more detailed information about the…
probably at the beach
  • 14,489
  • 16
  • 75
  • 116
686
votes
11 answers

Making Python loggers output all messages to stdout in addition to log file

Is there a way to make Python logging using the logging module automatically output things to stdout in addition to the log file where they are supposed to go? For example, I'd like all calls to logger.warning, logger.critical, logger.error to go to…
user248237
627
votes
10 answers

logger configuration to log to file and print to stdout

I'm using Python's logging module to log some debug strings to a file which works pretty well. Now in addition, I'd like to use this module to also print the strings out to stdout. How do I do this? In order to log my strings to a file I use…
stdcerr
  • 13,725
  • 25
  • 71
  • 128
508
votes
44 answers

How can I color Python logging output?

Some time ago, I saw a Mono application with colored output, presumably because of its log system (because all the messages were standardized). Now, Python has the logging module, which lets you specify a lot of options to customize output. So, I'm…
airmind
  • 7,361
  • 4
  • 18
  • 6
441
votes
13 answers

How do I disable log messages from the Requests library?

By default, the Requests python library writes log messages to the console, along the lines of: Starting new HTTP connection (1): example.com http://example.com:80 "GET / HTTP/1.1" 200 606 I'm usually not interested in these messages, and would…
aknuds1
  • 65,625
  • 67
  • 195
  • 317
408
votes
12 answers

Using logging in multiple modules

I have a small python project that has the following structure - Project -- pkg01 -- test01.py -- pkg02 -- test02.py -- logging.conf I plan to use the default logging module to print messages to stdout and a log file. To use the logging…
Quest Monger
  • 8,252
  • 11
  • 37
  • 43
319
votes
6 answers

How to Customize the time format for Python logging?

I am new to Python's logging package and plan to use it for my project. I would like to customize the time format to my taste. Here is a short code I copied from a tutorial: import logging # create logger logger =…
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
316
votes
23 answers

How should I log while using multiprocessing in Python?

Right now I have a central module in a framework that spawns multiple processes using the Python 2.6 multiprocessing module. Because it uses multiprocessing, there is module-level multiprocessing-aware log, LOG = multiprocessing.get_logger(). Per…
cdleary
  • 69,512
  • 53
  • 163
  • 191
297
votes
12 answers

How to write to a file, using the logging Python module?

How can I use the logging module in Python to write to a file? Every time I try to use it, it just prints out the message.
Takkun
  • 8,119
  • 13
  • 38
  • 41
268
votes
8 answers

How to obtain a Thread id in Python?

I have a multi-threading Python program, and a utility function, writeLog(message), that writes out a timestamp followed by the message. Unfortunately, the resultant log file gives no indication of which thread is generating which message. I would…
266
votes
16 answers

Python logging: use milliseconds in time format

By default logging.Formatter('%(asctime)s') prints with the following format: 2011-06-09 10:54:40,638 where 638 is the millisecond. I need to change the comma to a dot: 2011-06-09 10:54:40.638 To format the time I can…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
253
votes
10 answers

Logging uncaught exceptions in Python

How do you cause uncaught exceptions to output via the logging module rather than to stderr? I realize the best way to do this would be: try: raise Exception, 'Throwing a boring exception' except Exception, e: logging.exception(e) But my…
Jacob Marble
  • 28,555
  • 22
  • 67
  • 78
238
votes
7 answers

Logging within pytest tests

I would like to put some logging statements within test function to examine some state variables. I have the following code snippet: import pytest,os import logging logging.basicConfig(level=logging.DEBUG) mylogger =…
superselector
  • 3,007
  • 3
  • 19
  • 10
236
votes
8 answers

Python logging not outputting anything

In a python script I am writing, I am trying to log events using the logging module. I have the following code to configure my logger: ERROR_FORMAT = "%(levelname)s at %(asctime)s in %(funcName)s in %(filename) at line %(lineno)d:…
murgatroid99
  • 19,007
  • 10
  • 60
  • 95
1
2 3
58 59