-
Notifications
You must be signed in to change notification settings - Fork 0
/
wlan_logger.py
executable file
·87 lines (62 loc) · 2.3 KB
/
wlan_logger.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
#!/usr/bin/python2
# -*- coding: utf-8 -*-
# We're not logging for this module itself!
from __future__ import absolute_import, division, unicode_literals, print_function
import logging, logging.handlers
from datetime import datetime
import os
import sys
import time
from os.path import expanduser
HOME_DIR = "/home/pi"
class Logger(object):
def __init__( self, app_name, log_level, enable_console, enable_syslog, enable_file ):
self._app_name = app_name
self._logger = logging.getLogger( app_name )
self._logger.setLevel( log_level )
self._formatter = logging.Formatter( '%(name)s - %(asctime)s - %(levelname)s - %(message)s' )
if self._app_name != "":
if enable_console:
print( 'enable_console' )
_ch = logging.StreamHandler()
_ch.setLevel( log_level )
_ch.setFormatter( self._formatter )
self._logger.addHandler( _ch )
if enable_syslog:
print( 'enable_syslog' )
_sh = logging.handlers.SysLogHandler( address='/dev/log' )
_sh.setLevel( log_level )
_sh.setFormatter( logging.Formatter( '%(name)s: %(message)s' ) )
self._logger.addHandler( _sh )
if enable_file:
print( 'enable_file' )
_fh = logging.FileHandler( HOME_DIR + '/work/wlan-monitor/log/wlan_monitor.log' )
_fh.setLevel( log_level )
_fh.setFormatter( self._formatter )
self._logger.addHandler( _fh )
else:
print( "Logger() is not initialized." )
return
def get( self ):
return self._logger
# release mode - only with logging to syslog
logger = Logger( "wlanmonitor", logging.INFO, False, True, False )
# debug mode - with logging to console, syslog, file
#logger = Logger( "wlanmonitor", logging.DEBUG, True, True, True )
''' for test
log = logger.get()
def main( argv ):
#logger = Logger( "wlanmonitor", logging.INFO, False, True, True )
log = Logger( "wlanmonitor", logging.DEBUG, True, True, True ).get()
if( log == None ):
print( "Logger failed in initialization" )
log.debug( 'Hi, this is debug message.' )
log.info( 'Hi, this is info message.' )
log.warning( 'Hi, this is warning message.' )
log.critical( 'Hi, this is critical message.' )
log.error( 'Hi, this is error message.' )
return
if __name__ == "__main__":
main( sys.argv )
'''
# eof