-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.py
executable file
·119 lines (88 loc) · 3.87 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
DESCRIPTION
REQUIRED:
Include this module only in top level main module:
====================================================================
from log import logger
import logging
# This setLevel determines wich messages are passed on to lower handlers
logger.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, CRITICAL
====================================================================
In other modules, include snippet below:
====================================================================
import __main__
import logging
import os
script=os.path.basename(__main__.__file__)
script=os.path.splitext(script)[0]
logger = logging.getLogger(script + "." + __name__)
====================================================================
V1.1.0
31-10-2021
Disable syslog handler for non linux platforms
V0.1:
- initial version
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
__version__ = "1.1.0"
__author__ = "Hans IJntema"
__license__ = "GPLv3"
# ------------------------------------------------------------------------------------
# Logging
# ------------------------------------------------------------------------------------
import __main__
import logging
from logging.handlers import SysLogHandler
import os
import sys
import getpass
currentuser = getpass.getuser()
script=os.path.basename(__main__.__file__)
script=os.path.splitext(script)[0]
logger = logging.getLogger(script)
# This setLevel determines wich messages are passed on to lower handlers
logger.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, CRITICAL
logger.propagate = False
# Console stdout
c_handler = logging.StreamHandler(sys.stdout)
# This setLevel determines wich messages are processed by this handler (assuming it arrives from global logger
c_handler.setLevel(logging.DEBUG)
c_format = logging.Formatter('%(name)s %(levelname)s: FUNCTION:%(funcName)s LINE:%(lineno)d: %(message)s')
c_handler.setFormatter(c_format)
logger.addHandler(c_handler)
# Syslog
if sys.platform == "linux":
s_handler = logging.handlers.SysLogHandler( address=('/dev/log') )
# This setLevel determines wich messages are processed by this handler (assuming it arrives from global logger
s_handler.setLevel(logging.INFO)
s_format = logging.Formatter('%(name)s[%(process)d] %(levelname)s: %(asctime)s FUNCTION:%(funcName)s LINE:%(lineno)d: %(message)s', datefmt='%H:%M:%S')
s_handler.setFormatter(s_format)
logger.addHandler(s_handler)
# File
# Test if /dev/shm is writable otherwise use /tmp
try:
if os.access("/dev/shm", os.W_OK):
f_handler = logging.FileHandler(f"/dev/shm/{script}.{currentuser}.log", 'a')
else:
f_handler = logging.FileHandler(f"/tmp/{script}.{currentuser}.log", 'a')
f_handler.setLevel(logging.ERROR)
f_format = logging.Formatter('%(name)s[%(process)d] %(levelname)s: %(asctime)s FUNCTION:%(funcName)s LINE:%(lineno)d: %(message)s', datefmt='%Y-%m-%d,%H:%M:%S')
f_handler.setFormatter(f_format)
logger.addHandler(f_handler)
except:
print(f"ERROR: /dev/shm/{script}.log permission denied")
#logger.debug('This is a debug message')
#logger.info('This is an info message')
#logger.warning('This is a warning message')
#logger.error('This is an error message')
#logger.critical('This is a critical message')
#logger.exception("Exception occurred")