-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_decor.py
68 lines (57 loc) · 1.9 KB
/
logging_decor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import logging
from functools import wraps
from datetime import datetime
import requests
import os
time_host = "http://192.168.1.3:8020/time"
fpath = '/home/pi/mount/hampc/tp_logs'
#def get_time():
# return requests.get(time_host).json()['current_time']
def create_logger(name, level = logging.INFO):
"""
Creates a logging object and returns it
"""
# try:
# # get just the date for log files
# current_time = get_time()
# current_date = current_time.split(' ')[0]
# except Exception:
current_time = datetime.now()
current_date = str(current_time.strftime('%G-%m-%d'))
#fpath = os.path.join(os.path.dirname(__file__), 'logs/')
# create the logging file handler
logger = logging.getLogger(name)
logger.setLevel(level)
logpath = os.path.join(fpath, 'logs')
fh = logging.FileHandler(os.path.join(logpath,f'{current_date}_{name.upper()}.log'), mode='a+')
fmt = "%(asctime)s, %(name)s, %(levelname)s, %(threadName)s,\t%(message)s"
formatter = logging.Formatter(fmt, datefmt = "%d-%b-%G %H:%M:%S")
fh.setFormatter(formatter)
#add handler to logger object
logger.addHandler(fh)
return logger
#only for tipnouvs
def logit(logger):
"""
decorator tha twraps the passed in function and logs them
@param logger: the logging object
"""
def decorator_logit(func):
@wraps(func)
def wrapper_logit(args):
if 'error' in args:
logger.error(" | ".join([a for a in args if a != 'error']))
else:
if not isinstance(args, tuple):
logger.info(args)
else:
logger.info(" | ".join(args))
return func(args)
return wrapper_logit
return decorator_logit
global tipnovus_logger
tipnovus_logger = create_logger('tipnovus')
@logit(tipnovus_logger)
def handle_logs(args):
return
#print(args)