0

I am attempting to move some of my programs from Perl to Python. Our Perl logging configuration has the following line:

# perl_log.conf

log4perl.appender.LOGFILE.filename= sub { return get_perl_log() }

In each of our Perl scripts, we have this code block:

# my_perl_script.pl

my $perl_log = "/path/to/log.log";

# Instantiating logger object
sub get_perl_log{
  return $perl_log; 
}

I was wondering if it's possible to do this in Python. Currently, we have to create .conf files for each Python script, even though the fileHandlers are the same:

# my_python_script_log.conf

[formatters]
keys=simpleFormatter

[logger_root]
level=INFO
handlers=consoleHandler

[logger_my_python_script]
level=DEBUG
handlers=consoleHandler,fileHandler
qualname=my_python_script
propagate=0

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

[handler_fileHandler]
class=logging.handlers.TimedRotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=('/path/to/log/my_python_script.log','midnight',)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

I'd like to make this Python logging configuration accept a dynamic file name like:

args=(get_python_log())

Is this possible?

7
  • What do you mean dynamic and what is get_python_log() or get_perl_log() supposed to return? The logging docs show that the path is a string parameter to basicConfig() or FileHandler() and can come from anywhere Commented Apr 9 at 14:32
  • 1
    As for configuration, the docs show it can be loaded from a file or a dictionary. That dictionary in turn may come from any other format and can be modified before it's passed to dictConfig. Commented Apr 9 at 14:39
  • Those get functions just return the filepath to the log file Commented Apr 9 at 14:41
  • What you call perl_log.conf is actually code. This isn't "just". my_python_script_log.conf is only data though, just like JSON or YAML. It seems the real question is how to specify settings common for multiple handlers, not dynamic settings. Explicit coding or dictConfig can handle that. Commented Apr 9 at 14:43
  • Check this dict config example. This looks almost like a JSON file but is actually a Python dict. Some of those values could easily be other Python variables or method calls. Commented Apr 9 at 14:47

0

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.