cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
log.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.clm.utils.log
22 # @author Gaetano
23 # @date May 8, 2013
24 #
25 import logging
26 import os
27 from django.conf import settings
28 
29 
30 # # Set of active loggers - ones currently writing to logs.
31 active_loggers = set([])
32 
33 
34 ##
35 #
36 # Returns logger.
37 #
38 # - If no logger_id is provided, then logger is anonymus. His logs go to the file
39 # intended for storing anonimous logs.
40 #
41 # - Otherwise user is added to active loggers. Function creates unique file
42 # handler for his logs.
43 #
44 # This way actions may be analyzed per user.
45 #
46 def get_logger(logger_id):
47  if logger_id:
48  log_name = 'user_%d' % logger_id
49  else:
50  log_name = 'no_user'
51  logger_id = 0
52  if logger_id in active_loggers:
53  return logging.getLogger(log_name)
54 
55  active_loggers.add(logger_id)
56  logger = logging.getLogger(log_name)
57  hdlr = logging.handlers.TimedRotatingFileHandler(os.path.join(settings.LOG_DIR, '%s.log' % log_name), when='D', interval=1)
58  formatter = logging.Formatter(settings.LOG_FORMAT)
59  hdlr.setFormatter(formatter)
60  logger.addHandler(hdlr)
61  logger.setLevel(settings.LOG_LEVEL)
62  return logger
63 
64 
65 ##
66 #
67 # @parameter{logger_id,int} optional, id of the logger. If no id is provided,
68 # logs are anonymus.
69 # @parameter{text,string} content of the log.
70 #
71 # Prints debug log with @prm{contetnt} to log file of the user @prm{id}.
72 #
73 def debug(logger_id, text):
74  get_logger(logger_id).debug(text)
75 
76 
77 ##
78 #
79 # @parameter{logger_id,int} optional, id of the logger. If no id is provided,
80 # logs are anonymus.
81 # @parameter{text,string} content of the log.
82 #
83 # Prints info log with @prm{text} contetnt to log file of the user @prm{logger_id}.
84 #
85 def info(logger_id, text):
86  get_logger(logger_id).info(text)
87 
88 
89 ##
90 #
91 # @parameter{logger_id,int} optional, id of the logger. If no logger_id is provided,
92 # logs are anonymus.
93 # @parameter{text,string} content of the log.
94 #
95 # Prints warning log with @prm{text} contetnt to log file of the user @prm{logger_id}.
96 #
97 def warning(logger_id, text):
98  get_logger(logger_id).warning(text)
99 
100 
101 ##
102 #
103 # @parameter{logger_id,int} optional, id of the logger. If no id is provided,
104 # logs are anonymus.
105 # @parameter{text,string} content of the log.
106 #
107 # Prints exception log with @prm{text} contetnt to log file of the user @prm{logger_id}.
108 #
109 def exception(logger_id, text):
110  get_logger(logger_id).exception(text)
111 
112 
113 ##
114 #
115 # @parameter{logger_id,int} optional, id of the logger. If no id is provided,
116 # logs are anonymus.
117 # @parameter{text,string} content of the log.
118 #
119 # Prints error log with @prm{text} contetnt to log file of the user @prm{logger_id}.
120 #
121 def error(logger_id, text):
122  get_logger(logger_id).error(text)
123