annotate logging_module.py @ 4:901857c9b24f draft

Uploaded
author jaredgk
date Wed, 17 Oct 2018 17:30:37 -0400
parents 3830d29fca6a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
1 import sys
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
2 import logging
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
3
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
4
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
5
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
6 def initLogger(filename='pipeline.log', filelevel='INFO',
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
7 streamlevel='WARNING', resetlog=True):
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
8 """General logger initialization for PPP functions.
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
9
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
10 Messages from WARNING level and higher will be logged to file and to
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
11 stderr. By default, INFO level will also be written to logfile. Both
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
12 levels are flexible.
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
13
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
14 Level groupings:
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
15 ERROR: Error messages should be generated by an exception call
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
16 WARNING: Non-terminal behavior that may be unusual (i.e. lists
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
17 with no values, blank strings)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
18 INFO: Every major (user-facing) function should have the following:
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
19 -Message for function start
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
20 -List of input arguments and options
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
21 -Basic sanity checks (dimensions of input data)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
22 -Statements before or after major function calls
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
23 -Message for function end
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
24 DEBUG: Mainly for developer use/debugging. Generate logs for
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
25 sub-functions that match INFO level for major functions.
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
26 Possible care should be used if there are lots of loops called.
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
27
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
28 Use: Call with either the individual function (in __name__=="__main__"
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
29 statement) or in pipeline file.
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
30
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
31 Parameters
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
32 ----------
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
33 filename : str ("pipeline.log")
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
34 Name of file that log will be written to
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
35
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
36 filelevel : {'INFO','DEBUG','WARNING','ERROR'}
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
37 Set minimum level of log messages that are written to log file.
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
38 Note that this acts as a de facto minumum for 'streamlevel' as well.
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
39
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
40 streamlevel : {'WARNING','DEBUG','INFO','ERROR'}
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
41 Set minimum level of log messages that are output to stream.
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
42
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
43 resetlog : bool (True)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
44 If true, will overwrite logfile when opening. Set to false if log is
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
45 being initialized multiple times
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
46
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
47 Returns
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
48 -------
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
49 None
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
50
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
51 Exceptions
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
52 ----------
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
53 If filelevel or streamlevel are not a valid logging level
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
54
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
55 """
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
56 log_levels = ['DEBUG','INFO','WARNING','ERROR']
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
57 if filelevel is not None and filelevel.upper() not in log_levels:
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
58 raise Exception('filelevel value %s is not a valid level' %
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
59 filelevel)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
60 if streamlevel is not None and streamlevel.upper() not in log_levels:
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
61 raise Exception('streamlevel value %s is not a valid level' %
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
62 streamlevel)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
63 fmt_def = "%(asctime)s - %(funcName)s - %(levelname)s: %(message)s"
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
64 fmt_notime = "%(funcName)s - %(levelname)s: %(message)s"
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
65 fmtr = logging.Formatter(fmt=fmt_def)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
66 fmtr_notime = logging.Formatter(fmt=fmt_notime)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
67 filelogger = logging.getLogger()
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
68 filelogger.setLevel('INFO')
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
69 if streamlevel is not None:
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
70 s_handler = logging.StreamHandler()
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
71 s_handler.setFormatter(fmtr_notime)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
72 s_handler.setLevel(streamlevel)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
73 filelogger.addHandler(s_handler)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
74 logmode = 'a'
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
75 if resetlog:
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
76 logmode = 'w'
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
77 if filelevel is not None:
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
78 f_handler = logging.FileHandler(filename,mode=logmode)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
79 f_handler.setFormatter(fmtr)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
80 f_handler.setLevel(filelevel)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
81 #filelogger.setLevel(filelevel)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
82 filelogger.addHandler(f_handler)
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
83 #Formats exception messages to be sent to appropriate loggers
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
84 def exp_handler(etype,val,tb):
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
85 logging.error("%s" % (val), exc_info=(etype,val,tb))
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
86
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
87 sys.excepthook = exp_handler
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
88
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
89
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
90 def logArgs(args, func_name=None, print_nones=False):
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
91 header = "Arguments"
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
92 if func_name is not None:
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
93 header+=" for"+func_name
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
94 for arg in vars(args):
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
95 val = vars(args)[arg]
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
96 if val is not None or print_nones:
3830d29fca6a Uploaded
jaredgk
parents:
diff changeset
97 logging.info('Argument %s: %s' % (arg,val))