473,395 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

logging to two files

Hi

Have the following code:
import logging

logging.basicConfig(level = logging.DEBUG,
format = '[%(levelname)-8s %(asctime)s]
%(message)s',
filename = 'rfs.log',
filemode = 'w')

When using logging.(debug, info etc) stuff is logged to rfs.log.
How may I specify another log with different charateristics, such as a
different file

regards
Jul 19 '05 #1
2 3088
Tor Erik Sønvisen wrote:
Hi

Have the following code:
import logging

logging.basicConfig(level = logging.DEBUG,
format = '[%(levelname)-8s %(asctime)s]
%(message)s',
filename = 'rfs.log',
filemode = 'w')

When using logging.(debug, info etc) stuff is logged to rfs.log.
How may I specify another log with different charateristics, such as a
different file

regards

I'm not sure if I understood your problem. However, just a tip for you:
is it possible to create your own handler object? (See section 6.29.5 in
the library reference).
You could setup a handler object that holds a list of other handler
objects and distribute all logging events to them. This way you should
be able to add/remove handlers at runtime.

Best,

Laci 2.0

--
__________________________________________________ _______________
Laszlo Nagy web: http://designasign.biz
IT Consultant mail: ga*****@geochemsource.com

Python forever!
Jul 19 '05 #2
Tor Erik Sønvisen wrote:
Hi

Have the following code:
import logging

logging.basicConfig(level = logging.DEBUG,
format = '[%(levelname)-8s %(asctime)s]
%(message)s',
filename = 'rfs.log',
filemode = 'w')

When using logging.(debug, info etc) stuff is logged to rfs.log.
How may I specify another log with different charateristics, such as a
different file

regards

You have to not use basicConfig if you want multiple handlers. Instead set
up the configuration explicitly.

Here is some code I used recently which you can use as a base. It isn't
logging to two files, instead it logs to a file and the console.

# Initialise logging
def setupLogging():
SCRIPT = os.path.splitext(os.path.basename(sys.argv[0]))[0]
SECTNAME = 'logging-'+SCRIPT
if not configuration.has_section(SECTNAME):
SECTNAME = 'logging'

options = configuration.options(SECTNAME)
if 'level' in options:
loglevel = configuration.get(SECTNAME, 'level')
else:
loglevel = 'NOTSET'

if 'console' in options:
consolelevel = configuration.get(SECTNAME, 'console')
else:
consolelevel = ''

if isinstance(loglevel, basestring):
loglevel = logging._levelNames[loglevel.upper()]

if 'format' in options:
logformat = configuration.get(SECTNAME, 'format')
else:
logformat = '%(asctime)s %(levelname)s %(message)s'

if 'filename' in options:
logfile = configuration.get(SECTNAME, 'filename')
else:
logfile='errors.log'

if 'filemode' in options:
logmode = configuration.get(SECTNAME, 'filemode')
else:
logmode = 'a'

if isinstance(consolelevel, basestring):
clevel = logging._levelNames[consolelevel.upper()]
else:
clevel = consolelevel

handler = logging.FileHandler(logfile, "a")
fmt = logging.Formatter(logformat, "%Y-%m-%d %H:%M:%S")
handler.setFormatter(fmt)
handler.setLevel(loglevel)
logging.root.addHandler(handler)

if consolelevel:
console = logging.StreamHandler()
formatter = logging.Formatter('%(levelname)-8s %(message)s')
console.setFormatter(formatter)
console.setLevel(clevel)
logging.root.addHandler(console)

logging.root.setLevel(min(loglevel, clevel))
The configuration file contains a section such as:

[logging]
level=warning
filename=output\%(script)s.log
console=info

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ravi Shankar | last post by:
Hi all, I have an enterprise application. I am using Apache Log4J for the logging purposes. WHen a request is received by the application, it goes throug servlets and many classes, and the...
0
by: Neil Benn | last post by:
Hello, I'm running a test and having issues with logging, if I call logging.shutdown() and then want to start the logging going again then I get a problem as if I call shutdown, I can't get the...
6
by: Burkhard Schultheis | last post by:
As I wrote last week, we have a problem with a DB2 V8 on Linux. Here is what is in db2diag.log during online backup: Starting a full database backup. 2004-04-01-02.33.54.760164 ...
2
by: johnm | last post by:
As a refresher, a description of my problem is in the message body below. I could not get answers to all of the questions that were asked below in a previous thread, but here is what I found out:...
3
by: Karuppasamy | last post by:
Hi I am trying to use the Logging Module provided by Microsoft Application Blocks for .Net . I installed everything as per the Instructions given in the 'Development Using the Logging Block'. ...
3
by: nicholas.petrella | last post by:
I am currently trying to use the python logging system as a core enterprise level logging solution for our development and production environments. The rotating file handler seems to be what I...
3
by: Chris Shenton | last post by:
I am setting up handlers to log DEBUG and above to a rotating file and ERROR and above to console. But if any of my code calls a logger (e.g., logging.error("foo")) before I setup my handlers, the...
3
by: pundarikakshaiah | last post by:
Hi, I am using JDK1.4 logging framework and below are the properties that I have in my logging.properties file to configure logging.. handlers= java.util.logging.FileHandler,...
3
by: guybenron | last post by:
Hey, I have a sort of petty-neurotic question, I'm kinda pedantic and like to keep my log directories clean, and this thing is bothering me to the point of actually posting a question (couldn't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.