Package gofer :: Package agent :: Module logutil
[hide private]
[frames] | no frames]

Source Code for Module gofer.agent.logutil

 1  # 
 2  # Copyright (c) 2011 Red Hat, Inc. 
 3  # 
 4  # This software is licensed to you under the GNU Lesser General Public 
 5  # License as published by the Free Software Foundation; either version 
 6  # 2 of the License (LGPLv2) or (at your option) any later version. 
 7  # There is NO WARRANTY for this software, express or implied, 
 8  # including the implied warranties of MERCHANTABILITY, 
 9  # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should 
10  # have received a copy of LGPLv2 along with this software; if not, see 
11  # http://www.gnu.org/licenses/old-licenses/lgpl-2.0.txt. 
12  # 
13  # Jeff Ortel <jortel@redhat.com> 
14  # 
15   
16  import os 
17  import sys 
18  import logging 
19  from gofer import NAME 
20  from logging import root, Formatter 
21  from logging.handlers import RotatingFileHandler 
22   
23  LOGDIR = '/var/log/%s' % NAME 
24  LOGFILE = 'agent.log' 
25   
26  TIME = '%(asctime)s' 
27  LEVEL = ' [%(levelname)s]' 
28  THREAD = '[%(threadName)s]' 
29  FUNCTION = ' %(funcName)s()' 
30  FILE = ' @ %(filename)s' 
31  LINE = ':%(lineno)d' 
32  MSG = ' - %(message)s' 
33   
34  if sys.version_info < (2,5): 
35      FUNCTION = '' 
36   
37  FMT = \ 
38      ''.join((TIME, 
39              LEVEL, 
40              THREAD, 
41              FUNCTION, 
42              FILE, 
43              LINE, 
44              MSG,)) 
45   
46  handler = None 
47   
48 -def getLogger(name):
49 global handler 50 if not os.path.exists(LOGDIR): 51 os.mkdir(LOGDIR) 52 if handler is None: 53 path = os.path.join(LOGDIR, LOGFILE) 54 handler = RotatingFileHandler(path, maxBytes=0x100000, backupCount=5) 55 handler.setFormatter(Formatter(FMT)) 56 root.setLevel(logging.INFO) 57 root.addHandler(handler) 58 log = logging.getLogger(name) 59 return log
60