source: branches/numpy/anuga/utilities/log.py @ 6553

Last change on this file since 6553 was 6553, checked in by rwilson, 15 years ago

Merged trunk into numpy, all tests and validations work.

  • Property svn:executable set to *
File size: 4.1 KB
Line 
1#!/usr/bin/env python
2
3'''
4A simple logging module that logs to the console and a logfile, and has a
5configurable threshold loglevel for each of console and logfile output.
6
7Use 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
12This class uses the 'borg' pattern - there is never more than one instance
13of log data.  See
14<http://www.suttoncourtenay.org.uk/duncan/accu/pythonpatterns.html>
15for the basic idea used here: modules *are* singletons!
16
17Until the first call to log() the user is free to play with the module data
18to configure the logging.
19'''
20
21import sys
22import os.path
23import traceback
24import 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
35console_logging_level = logging.INFO
36
37# logging level for the logfile
38log_logging_level = logging.DEBUG
39
40# The name of the file to log to.
41log_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.
53def 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.
105def 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.
111def info(msg):
112    log(logging.INFO, msg)
113
114##
115# @brief Shortcut for log(WARNING, msg).
116# @param msg Message string to log at logging.WARNING level.
117def warning(msg):
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.
123def 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.
129def critical(msg):
130    log(logging.CRITICAL, msg)
131
Note: See TracBrowser for help on using the repository browser.