1

Let's say I have the following config log file:

[loggers]
keys=root,seeker,event

[handlers]
keys=consoleHandler,seekerFileHandler,eventFileHandler

[formatters]
keys=consoleFormatter,logFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler,seekerFileHandler,eventFileHandler

[logger_seeker]
level=DEBUG
handlers=consoleHandler,seekerFileHandler
qualname=seeker
propagate=0

[logger_event]
level=DEBUG
handlers=consoleHandler,eventFileHandler
qualname=event
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=consoleFormatter
args=(sys.stdout,)

[handler_seekerFileHandler]
class=FileHandler
level=DEBUG
formatter=logFormatter
args=('seeker.log','a')

[handler_eventFileHandler]
class=FileHandler
level=DEBUG
formatter=logFormatter
args=('event.log','a')

[formatter_consoleFormatter]
format=%(asctime)s - thread:%(thread)d - %(name)s - %(levelname)s | %(message)s
datefmt=%m/%d/%Y %X

[formatter_logFormatter]
format=%(asctime)s | %(message)s
datefmt=%m/%d/%Y %X

Typically I would:

import logging
from logging.config import fileConfig
from os import getcwd

fileConfig(''.join([getcwd(),'/logging.conf']))
event_logger = logging.getLogger("event")
seeker_logger = logging.getLogger("seeker")

to handle each logger. However, I tend to run this software on two separate platforms: Windows and Linux so it would be nice if they each saved it in a "common" location. What I am looking for is something like:

from sys import platform
if 'win' in platform:
    #alter the save path to this location
if 'linux' in platform:
    #alter save path to this location

but I have no idea how to implement this with a config file, any ideas?

4
  • BTW, why not just use fileConfig('logging.conf')? Commented Oct 21, 2012 at 4:47
  • 1
    I don't think you can do it declaratively with the config file, so either you have two, mostly similar, configuration files for each platform and choose which one to load based on the platform, or you monkey patch the file handler object after loading the generic configuration to set the correct destination file. Commented Oct 21, 2012 at 8:49
  • ...getcwd is the current directory; there's really no point (you gain zero additional safety). Commented Feb 7, 2013 at 22:45
  • @nneonneo hey you won, you came in and created an off-topic beratement in a help forum, good job. Commented Feb 12, 2013 at 9:11

1 Answer 1

1

You have 2 options.

  1. Use a logging-linux.conffile and a logging-win.conffile with your differents path and load them inside your plateform tests.

  2. Instead of using a config file, delegate the creation of logger to your own module and make the plateform test when creating the FileHandler instances.

The solution to adopt will depend on the complexity of your code. If you are building a library, take a look at this page: http://docs.python.org/howto/logging.html#configuring-logging-for-a-library

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, so there is no way to blend the 2? Either I keep two config file or do everything through code which will make me lose the run time level altering benefit of having a config file... oh well thank you.
I don't think so ... I've run through similar problems and never found a solution to mix the two approach decently.

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.