1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | ''' |
---|
4 | A simple logging module that logs to the console and a logfile, and has a |
---|
5 | configurable threshold loglevel for each of console and logfile output. |
---|
6 | |
---|
7 | Use it this way: |
---|
8 | import anuga.utilities.log as log |
---|
9 | log.debug('A message at DEBUG level') |
---|
10 | log.info('Another message, INFO level') |
---|
11 | |
---|
12 | This class uses the 'borg' pattern - there is never more than one instance |
---|
13 | of log data. See |
---|
14 | <http://www.suttoncourtenay.org.uk/duncan/accu/pythonpatterns.html> |
---|
15 | for the basic idea used here: modules *are* singletons! |
---|
16 | |
---|
17 | Until the first call to log() the user is free to play with the module data |
---|
18 | to configure the logging. |
---|
19 | ''' |
---|
20 | |
---|
21 | import sys |
---|
22 | import os.path |
---|
23 | import traceback |
---|
24 | import logging |
---|
25 | |
---|
26 | |
---|
27 | ################################################################################ |
---|
28 | # Module variables - only one copy of these, ever. |
---|
29 | ################################################################################ |
---|
30 | |
---|
31 | # flag variable to determine if logging set up or not |
---|
32 | _setup = False |
---|
33 | |
---|
34 | # logging level for the console |
---|
35 | console_logging_level = logging.INFO |
---|
36 | |
---|
37 | # logging level for the logfile |
---|
38 | log_logging_level = logging.DEBUG |
---|
39 | |
---|
40 | # The name of the file to log to. |
---|
41 | log_filename = './anuga.log' |
---|
42 | |
---|
43 | |
---|
44 | ################################################################################ |
---|
45 | # Module code. |
---|
46 | ################################################################################ |
---|
47 | |
---|
48 | ## |
---|
49 | # @brief Log a message at a specified level. |
---|
50 | # @param level The loglevel to log with (logging.DEBUG, etc). |
---|
51 | # @param msg Message string to log. |
---|
52 | # @note First call of this method initializes the logging system. |
---|
53 | def log(level, msg): |
---|
54 | '''Log a message at a particular loglevel. |
---|
55 | |
---|
56 | The first call to this method (by anybody) initializes logging and |
---|
57 | then logs the message. Subsequent calls just log the message. |
---|
58 | ''' |
---|
59 | |
---|
60 | global _setup |
---|
61 | |
---|
62 | # have we been setup? |
---|
63 | if not _setup: |
---|
64 | # setup the file logging system |
---|
65 | fmt = '%(asctime)s %(levelname)-8s %(mname)25s:%(lnum)-4d|%(message)s' |
---|
66 | logging.basicConfig(level=log_logging_level, format=fmt, |
---|
67 | filename=log_filename, filemode='w') |
---|
68 | |
---|
69 | # define a console handler which writes to sys.stdout |
---|
70 | console = logging.StreamHandler(sys.stdout) |
---|
71 | console.setLevel(console_logging_level) |
---|
72 | formatter = logging.Formatter('%(message)s') |
---|
73 | console.setFormatter(formatter) |
---|
74 | logging.getLogger('').addHandler(console) |
---|
75 | |
---|
76 | # tell the world how we are set up |
---|
77 | start_msg = ("Logfile is '%s' with logging level of %s, " |
---|
78 | "console logging level is %s" |
---|
79 | % (log_filename, |
---|
80 | logging.getLevelName(log_logging_level), |
---|
81 | logging.getLevelName(console_logging_level))) |
---|
82 | logging.log(logging.CRITICAL, start_msg, |
---|
83 | extra={'mname': __name__, 'lnum': 0}) |
---|
84 | |
---|
85 | # mark module as *setup* |
---|
86 | _setup = True |
---|
87 | |
---|
88 | # get caller information - look back for first module != <this module name> |
---|
89 | frames = traceback.extract_stack() |
---|
90 | frames.reverse() |
---|
91 | for (mname, lnum, _, _) in frames: |
---|
92 | mname = os.path.basename(mname).rsplit('.', 1)[0] |
---|
93 | if mname != __name__: |
---|
94 | break |
---|
95 | |
---|
96 | logging.log(level, msg, extra={'mname': mname, 'lnum': lnum}) |
---|
97 | |
---|
98 | ################################################################################ |
---|
99 | # Shortcut routines to make for simpler user code. |
---|
100 | ################################################################################ |
---|
101 | |
---|
102 | ## |
---|
103 | # @brief Shortcut for log(DEBUG, msg). |
---|
104 | # @param msg Message string to log at logging.DEBUG level. |
---|
105 | def debug(msg): |
---|
106 | log(logging.DEBUG, msg) |
---|
107 | |
---|
108 | ## |
---|
109 | # @brief Shortcut for log(INFO, msg). |
---|
110 | # @param msg Message string to log at logging.INFO level. |
---|
111 | def info(msg): |
---|
112 | log(logging.INFO, msg) |
---|
113 | |
---|
114 | ## |
---|
115 | # @brief Shortcut for log(WARNING, msg). |
---|
116 | def warning(msg): |
---|
117 | # @param msg Message string to log at logging.WARNING level. |
---|
118 | log(logging.WARNING, msg) |
---|
119 | |
---|
120 | ## |
---|
121 | # @brief Shortcut for log(ERROR, msg). |
---|
122 | # @param msg Message string to log at logging.ERROR level. |
---|
123 | def error(msg): |
---|
124 | log(logging.ERROR, msg) |
---|
125 | |
---|
126 | ## |
---|
127 | # @brief Shortcut for log(CRITICAL, msg). |
---|
128 | # @param msg Message string to log at logging.CRITICAL level. |
---|
129 | def critical(msg): |
---|
130 | log(logging.CRITICAL, msg) |
---|
131 | |
---|