-
Notifications
You must be signed in to change notification settings - Fork 3
/
log.py
92 lines (75 loc) · 2.46 KB
/
log.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
"""
Logging set-up.
NOTE: kivy sets the root logger to some strange and buggy behavior,
a patch is necessary: comment out 'logging.root = Logger' in kivy/logger.py
"""
__author__ = 'Holger Fleischmann'
__copyright__ = 'Copyright 2018, Holger Fleischmann, Bavaria/Germany'
__license__ = 'Apache License 2.0'
import logging.config
import os
import psutil
import subprocess
import utils
os.makedirs('logs', 0o775, True)
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': '%(asctime)s %(levelname)-8.8s [%(threadName)-12.12s] %(name)-13.13s: %(message)s [%(filename)s:%(lineno)d]',
'datefmt': "%Y-%m-%d %H:%M:%S",
}
},
'handlers': {
'console': {
'level': 'DEBUG',
'formatter': 'standard',
'class': 'logging.StreamHandler',
},
'rotate_file': {
'level': 'DEBUG',
'formatter': 'standard',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'logs/datalogger.log',
'encoding': 'utf8',
'maxBytes': 1000000,
'backupCount': 9,
}
},
'loggers': {
'': {
'handlers': ['rotate_file'],
'level': 'DEBUG',
},
}
}
logging.config.dictConfig(LOGGING)
logger = logging.getLogger().getChild(__name__)
logger.info('''
--------------------------------------------------------------------
--- DATALOGGER APPLICATION RESTART --------------------------------
--------------------------------------------------------------------
''')
# ensure that the header has been printed before any other output
for handler in logger.handlers:
handler.flush()
def _tick_out() -> None:
try:
cpu_temp = subprocess.check_output(
'vcgencmd measure_temp',
stderr=subprocess.STDOUT,
shell=True).decode('latin-1').replace('\n', '').replace("'", "°")
except:
cpu_temp = 'temp unknown'
try:
this_process = psutil.Process(os.getpid())
mem_usage = 'VM {:.1f}MB, RSS {:.1f}MB'.format(
this_process.memory_info().vms / 1e6,
this_process.memory_info().rss / 1e6)
except:
mem_usage = 'mem unknown'
logging.debug('TICK - CPU ' + cpu_temp + ', memory usage ' + mem_usage)
# Setup log timer tick to support diagnostics of power supply problems:
utils.RepeatTimer(60, _tick_out).start()