|
|
@ -17,16 +17,22 @@ import os
|
|
|
|
import socket
|
|
|
|
import socket
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FORMAT_STR = '[%(levelname)s %(asctime)s %(filename)s:%(lineno)d] %(message)s'
|
|
|
|
|
|
|
|
DATE_FMT_STR = '%Y/%m/%d %H:%M:%S'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
|
|
|
level=logging.DEBUG, format=FORMAT_STR, datefmt=DATE_FMT_STR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_log_dir(log_dir=None):
|
|
|
|
def find_log_dir(log_dir=None):
|
|
|
|
"""Returns the most suitable directory to put log files into.
|
|
|
|
"""Returns the most suitable directory to put log files into.
|
|
|
|
Args:
|
|
|
|
Args:
|
|
|
|
log_dir: str|None, if specified, the logfile(s) will be created in that
|
|
|
|
log_dir: str|None, if specified, the logfile(s) will be created in that
|
|
|
|
directory. Otherwise if the --log_dir command-line flag is provided,
|
|
|
|
directory. Otherwise if the --log_dir command-line flag is provided,
|
|
|
|
the logfile will be created in that directory. Otherwise the logfile
|
|
|
|
the logfile will be created in that directory. Otherwise the logfile
|
|
|
|
will be created in a standard location.
|
|
|
|
will be created in a standard location.
|
|
|
|
Raises:
|
|
|
|
Raises:
|
|
|
|
FileNotFoundError: raised when it cannot find a log directory.
|
|
|
|
FileNotFoundError: raised when it cannot find a log directory.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
# Get a list of possible log dirs (will try to use them in order).
|
|
|
|
# Get a list of possible log dirs (will try to use them in order).
|
|
|
|
if log_dir:
|
|
|
|
if log_dir:
|
|
|
@ -45,22 +51,22 @@ def find_log_dir(log_dir=None):
|
|
|
|
|
|
|
|
|
|
|
|
def find_log_dir_and_names(program_name=None, log_dir=None):
|
|
|
|
def find_log_dir_and_names(program_name=None, log_dir=None):
|
|
|
|
"""Computes the directory and filename prefix for log file.
|
|
|
|
"""Computes the directory and filename prefix for log file.
|
|
|
|
Args:
|
|
|
|
Args:
|
|
|
|
program_name: str|None, the filename part of the path to the program that
|
|
|
|
program_name: str|None, the filename part of the path to the program that
|
|
|
|
is running without its extension. e.g: if your program is called
|
|
|
|
is running without its extension. e.g: if your program is called
|
|
|
|
'usr/bin/foobar.py' this method should probably be called with
|
|
|
|
'usr/bin/foobar.py' this method should probably be called with
|
|
|
|
program_name='foobar' However, this is just a convention, you can
|
|
|
|
program_name='foobar' However, this is just a convention, you can
|
|
|
|
pass in any string you want, and it will be used as part of the
|
|
|
|
pass in any string you want, and it will be used as part of the
|
|
|
|
log filename. If you don't pass in anything, the default behavior
|
|
|
|
log filename. If you don't pass in anything, the default behavior
|
|
|
|
is as described in the example. In python standard logging mode,
|
|
|
|
is as described in the example. In python standard logging mode,
|
|
|
|
the program_name will be prepended with py_ if it is the program_name
|
|
|
|
the program_name will be prepended with py_ if it is the program_name
|
|
|
|
argument is omitted.
|
|
|
|
argument is omitted.
|
|
|
|
log_dir: str|None, the desired log directory.
|
|
|
|
log_dir: str|None, the desired log directory.
|
|
|
|
Returns:
|
|
|
|
Returns:
|
|
|
|
(log_dir, file_prefix, symlink_prefix)
|
|
|
|
(log_dir, file_prefix, symlink_prefix)
|
|
|
|
Raises:
|
|
|
|
Raises:
|
|
|
|
FileNotFoundError: raised in Python 3 when it cannot find a log directory.
|
|
|
|
FileNotFoundError: raised in Python 3 when it cannot find a log directory.
|
|
|
|
OSError: raised in Python 2 when it cannot find a log directory.
|
|
|
|
OSError: raised in Python 2 when it cannot find a log directory.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
if not program_name:
|
|
|
|
if not program_name:
|
|
|
|
# Strip the extension (foobar.par becomes foobar, and
|
|
|
|
# Strip the extension (foobar.par becomes foobar, and
|
|
|
@ -123,12 +129,10 @@ class Log():
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if not self.logger.hasHandlers():
|
|
|
|
if not self.logger.hasHandlers():
|
|
|
|
format = '[%(levelname)s %(asctime)s %(filename)s:%(lineno)d] %(message)s'
|
|
|
|
formatter = logging.Formatter(fmt=FORMAT_STR, datefmt=DATE_FMT_STR)
|
|
|
|
formatter = logging.Formatter(
|
|
|
|
|
|
|
|
fmt=format, datefmt='%Y/%m/%d %H:%M:%S')
|
|
|
|
|
|
|
|
fh = logging.FileHandler(Log.log_name)
|
|
|
|
fh = logging.FileHandler(Log.log_name)
|
|
|
|
fh.setFormatter(formatter)
|
|
|
|
|
|
|
|
fh.setLevel(logging.DEBUG)
|
|
|
|
fh.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
fh.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(fh)
|
|
|
|
self.logger.addHandler(fh)
|
|
|
|
|
|
|
|
|
|
|
|
ch = logging.StreamHandler()
|
|
|
|
ch = logging.StreamHandler()
|
|
|
@ -136,9 +140,6 @@ class Log():
|
|
|
|
ch.setFormatter(formatter)
|
|
|
|
ch.setFormatter(formatter)
|
|
|
|
self.logger.addHandler(ch)
|
|
|
|
self.logger.addHandler(ch)
|
|
|
|
|
|
|
|
|
|
|
|
#fh.close()
|
|
|
|
|
|
|
|
#ch.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# stop propagate for propagating may print
|
|
|
|
# stop propagate for propagating may print
|
|
|
|
# log multiple times
|
|
|
|
# log multiple times
|
|
|
|
self.logger.propagate = False
|
|
|
|
self.logger.propagate = False
|
|
|
|