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.cm.utils.log
22 #
23 
24 import logging
25 import os
26 from django.conf import settings
27 
28 active_loggers = set([])
29 
30 
31 def get_logger(logger_id):
32  if logger_id:
33  log_name = 'user_%s' % str(logger_id)
34  else:
35  log_name = 'no_user'
36  logger_id = 0
37  if logger_id in active_loggers:
38  return logging.getLogger(log_name)
39 
40  active_loggers.add(logger_id)
41  logger = logging.getLogger(log_name)
42  hdlr = logging.handlers.TimedRotatingFileHandler(os.path.join(settings.LOG_DIR, '%s.log' % log_name), when='D', interval=1)
43  formatter = logging.Formatter(settings.LOG_FORMAT)
44  hdlr.setFormatter(formatter)
45  logger.addHandler(hdlr)
46  logger.setLevel(settings.LOG_LEVEL)
47  return logger
48 
49 
50 def debug(logger_id, text):
51  get_logger(logger_id).debug(text)
52 
53 
54 def info(logger_id, text):
55  get_logger(logger_id).info(text)
56 
57 
58 def warning(logger_id, text):
59  get_logger(logger_id).warning(text)
60 
61 
62 def exception(logger_id, text):
63  get_logger(logger_id).exception(text)
64 
65 
66 def error(logger_id, text):
67  get_logger(logger_id).error(text)
68 
69 
70 active_ctx_loggers = set([])
71 
72 
73 def get_ctx_logger(logger_id):
74  if logger_id:
75  log_name = 'ip_%s' % str(logger_id)
76  else:
77  log_name = 'no_ip'
78  logger_id = 0
79  if logger_id in active_ctx_loggers:
80  return logging.getLogger(log_name)
81 
82  active_ctx_loggers.add(logger_id)
83  logger = logging.getLogger(log_name)
84  hdlr = logging.handlers.TimedRotatingFileHandler(os.path.join(settings.LOG_DIR, '%s.log' % log_name), when='D', interval=1)
85  formatter = logging.Formatter(settings.LOG_FORMAT)
86  hdlr.setFormatter(formatter)
87  logger.addHandler(hdlr)
88  logger.setLevel(settings.LOG_LEVEL)
89  return logger
90 
91 
92 def ctx_debug(logger_id, text):
93  get_ctx_logger(logger_id).debug(text)
94 
95 
96 def ctx_info(logger_id, text):
97  get_ctx_logger(logger_id).info(text)
98 
99 
100 def ctx_warning(logger_id, text):
101  get_ctx_logger(logger_id).warning(text)
102 
103 
104 def ctx_exception(logger_id, text):
105  get_ctx_logger(logger_id).exception(text)
106 
107 
108 def ctx_error(logger_id, text):
109  get_ctx_logger(logger_id).error(text)
110 
111 
112 active_thread_loggers = set([])
113 
114 
115 def get_thread_logger(logger_id):
116  if logger_id:
117  log_name = 'thread_%d' % logger_id
118  else:
119  log_name = 'no_thread'
120  logger_id = 0
121  if logger_id in active_thread_loggers:
122  return logging.getLogger(log_name)
123 
124  active_thread_loggers.add(logger_id)
125  logger = logging.getLogger(log_name)
126  hdlr = logging.handlers.TimedRotatingFileHandler(os.path.join(settings.LOG_DIR, '%s.log' % log_name), when='D', interval=1)
127  formatter = logging.Formatter(settings.LOG_FORMAT)
128  hdlr.setFormatter(formatter)
129  logger.addHandler(hdlr)
130  logger.setLevel(settings.LOG_LEVEL)
131  return logger
132 
133 
134 def thread_debug(logger_id, text):
135  get_thread_logger(logger_id).debug(text)
136 
137 
138 def thread_info(logger_id, text):
139  get_thread_logger(logger_id).info(text)
140 
141 
142 def thread_warning(logger_id, text):
143  get_thread_logger(logger_id).warning(text)
144 
145 
146 def thread_exception(logger_id, text):
147  get_thread_logger(logger_id).exception(text)
148 
149 
150 def thread_error(logger_id, text):
151  get_thread_logger(logger_id).error(text)
152