Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions config/ex7.log.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# formatter methods - defined in utils module
# main file - write
# main file - append
# error file - JSON - append
#
version: 1
log_root: '|'
log_dir: 'logs'
timestamped: 1
timestamp_format: '-%Y%m%d'
#datestamped: 1
formatters:
cons:
format: '%(name)s, %(levelname)s - %(message)s'
Expand All @@ -25,8 +27,6 @@ handlers:
formatter: file
filename: 'ex7.log'
mode: 'a'


err:
class: logging.FileHandler
level: WARN
Expand Down
3 changes: 3 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,13 @@ log-yaml6: install log1.py ex6.log.yml
LOG_CONFIG=ex6.log.yml python log1.py
ls -l err.log

# date-stampped? time-stampped
#
log-yaml7: install log1.py ex7.log.yml
LOG_CONFIG=ex7.log.yml python log1.py
ls -l err.log

# timed-rotate
log-yaml8: install log1.py ex8.log.yml
LOG_CONFIG=ex8.log.yml python log1.py
ls -l err.log logs/ex8*
Expand Down
7 changes: 5 additions & 2 deletions scripts/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import re
from utils import logger
from sample import sample
from datetime import datetime

def main():
ltype = 'basic'
Expand Down Expand Up @@ -44,7 +45,7 @@ def main():
return 1

print("got here ")
log.warning("BEGINS")
log.warning(f"BEGINS {__name__} now={datetime.now()}")
log.debug('debug ')

log.info("some info")
Expand All @@ -55,11 +56,13 @@ def main():

sample.func1()

print("Handlers: ")
logger.list_handlers()
logger.list_root_handlers()

print(f"wrote: {logger.log_file}")

return 0
return

if __name__ == '__main__':
#status = main()
Expand Down
7 changes: 5 additions & 2 deletions scripts/log1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

import sys
from utils import logger_yaml

from sample import sample

log = logger_yaml.get_mod_logger()

def main():
Expand All @@ -10,10 +13,10 @@ def main():

log.error("something bad ")

log.info("done ")

log.critical("failed")
log.info("done ")

sample.func1()

if __name__ == '__main__':
status = main()
Expand Down
43 changes: 32 additions & 11 deletions utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,21 @@ class MyFormatter(logging.Formatter):

def format(self, record):
#print("rec=" + str(dir(record)) )
record.asctime = self.formatTime(record)
#record.asctime = self.formatTime(record)
record.asctime = self.formatTime(record, "%Y-%m-%d-%H-%M-%S")

#k = record._dict__.keys()
# debug
#return "... %s - %s - %s" % (record.name, record.levelname, 'msg')
#return "... %s - %s - %s" % (record.name, record.levelname, record.message )
#return "... %s - %s - %s keys=%s" % (record.name, record.levelname, record.msg, type(record))
return "%s - %s - %s - %s IN %s" % (self.formatTime(record), record.name, record.levelname, record.msg, record.module )
return f"{self.formatTime(record)} - {record.name} - {record.levelname} - {record.msg} - {record.module}"
return f"{record.asctime} - {record.name} - {record.levelname} - {record.msg} - [ {record.filename}: {record.funcName} ] "

return "%s - %s - %s - %s IN %s [] " % ( record.asctime, record.name, record.levelname, record.msg, record.module )

return "%s - %s - %s - %s IN %s [] " % (self.formatTime(record), record.name, record.levelname, record.msg, record.module )


#return "%s - %s - %s - %s keys=%s" % (record.asctime, record.name, record.levelname, record.msg, type(record))
#return "%s - %s - %s - %s IN %s: %s #%s" % (record.asctime, record.name, record.levelname, record.msg, record.funcName, record.filename, record.lineno)

Expand All @@ -70,12 +77,14 @@ def get_mod_logger(mod_name=None):

def init_basic(fname='basic.log'):
"""basic logging, level = DEBUG, log file = ??? """
global log_root
global log_root, logger, log_file

log_root = '|'
fmt="%(funcName)s():%(lineno)i: %(message)s %(levelname)s"
logging.basicConfig(level=logging.DEBUG, format=fmt, filename=fname)
return get_mod_logger()
log_file = fname
logger = get_mod_logger()
return logger
###return logging.getLogger(log_root)

def init_logging_ini(config_file, root_name=None):
Expand All @@ -87,7 +96,7 @@ def init_logging_yaml(config_file, root_name=None):
"""initialize logging configuration via a dict specified from a YAML file """
# https://docs.python.org/3/library/logging.config.html#logging-config-api

global log_root
global log_root, timestamp_format
yaml=YAML(typ='safe') # default, if not specfied, is 'rt' (round-trip)
with open(config_file) as fh:
text = fh.read()
Expand All @@ -100,13 +109,15 @@ def init_logging_yaml(config_file, root_name=None):
cfg['loggers'][root_name] = cfg['loggers'][log_root]
log_root = root_name.upper()

t = cfg.get('timestamped', 0)
print(f" t? {t} ")
label = None
timestamp_format = cfg.get('timestamp_format', timestamp_format)
print(f"ts format={timestamp_format}" )

if cfg.get('timestamped', 0):
f = cfg['handlers']['file']['filename']
ts = datetime.strftime(timestamp_format)
f = re.sub('.', '.' + ts, f)
print(f"replace {cfg['handlers']['file']['filename']} ")
#print(f"replace {cfg['handlers']['file']['filename']} ")
cfg['handlers']['file']['filename'] = f

if log_file:
Expand All @@ -127,6 +138,8 @@ def init_logging_yaml(config_file, root_name=None):
def list_root_handlers():
for h in logging.root.handlers[:]:
print(f"root: h={h}" )
name = h.__dict__.get('baseFilename')
print(f"name={name}" )

def list_handlers1():
# for h in logging.Logger.manager.loggerDict.keys()
Expand All @@ -136,9 +149,17 @@ def list_handlers1():
print(f"f={h.filename}")

def list_handlers():

for h in logger.handlers:
print(f"got h={h} ")
l = h.level
print(f"got h={h} l={l}")

for a in h.__dict__.keys():
print(f" key: {a}" )

name = h._name
name = h.__dict__.get('baseFilename')
print(f"name={name}" )

if hasattr(h, 'filename'):
print(f"f={h.filename}")

Expand Down
6 changes: 5 additions & 1 deletion utils/logger_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def init_logging_yaml(config_file):
"""initialize logging configuration via a dict specified from a YAML file """
# https://docs.python.org/3/library/logging.config.html#logging-config-api

global log_root, log_file, log_dir
global log_root, log_file, log_dir, timestamp_format
yaml=YAML(typ='safe') # default, if not specfied, is 'rt' (round-trip)
with open(config_file) as fh:
text = fh.read()
Expand Down Expand Up @@ -87,6 +87,10 @@ def init_logging_yaml(config_file):
#print(f"log level={l['level']}")

log_file = cfg['handlers']['file']['filename']
if 'timestamp_format' in cfg:
timestamp_format = cfg['timestamp_format']
#timestamp_format = cfg.get('timestamp_format', timestamp_format)
print(f"ts format={timestamp_format}" )

t = cfg.get('timestamped', 0)
if cfg.get('timestamped', 0):
Expand Down