The ** logging ** package that comes standard is easy to use, but it's reasonable. I feel like the file handler isn't killed until the script dies. The following is a simple example of personally unexpected behavior. (Maybe this is the correct behavior ... Tell me more!)
sample_logging.py
from datetime import datetime as dt
import os, logging
def main():
for i in xrange(1,6):
log = logging.getLogger()
#i-th iteration directory
dir_name = "dir_{0}".format(i)
os.makedirs(dir_name)
#Log file (want to create) for i-th repeat directory
log_file_name = "{0}/lg_index{1}.log".format(dir_name, i)
logging.basicConfig(level = logging.INFO,\
filename = log_file_name,\
format = "[%(name)s: %(levelname)s @ %(asctime)s] %(message)s")
# log
log.info('hoge')
log.info('foo')
log.info(dt.now().strftime('%Y%m%d%H%M%S))
As a feeling, in each directory of dir_1, dir_2, ..., dir_5 I wanted to write a log when I did something, but when I do this In the log file (dir_1 / lg_index1.log "of dir_1 where logging was set first All info (because level = logging.INFO in this case) is written out. (This usage may not be correct / cool in the first place.)
I referred to this (* I've been squeezed by copying)
sample_logging_new.py
from datetime import datetime as dt
import os, logging
def main():
for i in xrange(1,6):
log = logging.getLogger()
#i-th iteration directory
dir_name = "dir_{0}".format(i)
os.makedirs(dir_name)
#Log file (want to create) for i-th repeat directory
log_file_name = "{0}/lg_index{1}.log".format(dir_name, i)
log.setLevel(logging.INFO)
fh = logging.FileHandler(filename = log_file_name)
formatter = logging.Formatter(
fmt='"[%(name)s: %(levelname)s @ %(asctime)s] %(message)s"',
datefmt='%Y-%m-%d %H:%M:%S')
fh.setFormatter(formatter)
log.addHandler(fh)
# log
log.info('hoge')
log.info('foo')
log.info(dt.now().strftime('%Y%m%d%H%M%S))
# close file handler
log.removeHandler(fh)
del log, fh
@shima__shima taught me.
sample_logging_json.py
from datetime import datetime as dt
import os, json
def main():
for i in xrange(1,6):
#i-th iteration directory
info = {} #Dictionary to put data
dir_name = "dir_{0}".format(i)
os.makedirs(dir_name)
#Log file (want to create) for i-th repeat directory
log_file_name = "{0}/lg_index{1}.log".format(dir_name, i)
info['hoge'] = 0
info['foo'] = 0
info['bar'] = dt.now().strftime('%Y%m%d%H%M%S)
#json file export
with open(log_file_name, 'w') as f:
json.dump(info, f)
I thought that I didn't have to use logging if I just wanted to write out information (parameters, time, etc.).
Recommended Posts