Przeglądaj źródła

[log] Default log level to INFO in all handlers (#3518)

Ying Chen 2 lat temu
rodzic
commit
ea3a7bacdb
2 zmienionych plików z 129 dodań i 33 usunięć
  1. 97 0
      desktop/conf/dev_log.conf
  2. 32 33
      desktop/core/src/desktop/log/__init__.py

+ 97 - 0
desktop/conf/dev_log.conf

@@ -0,0 +1,97 @@
+########################################
+# Definition for the different objects
+# - FOR DEVELOPMENT ONLY -
+#
+# Directories where log files are kept must already exist.
+# That's why we pick /tmp.
+#
+# The loggers are configured to write to the log files ONLY.
+# Developers may set the DESKTOP_DEBUG environment variable to
+# enable stderr logging output.
+########################################
+
+[logger_root]
+handlers=logfile,errorlog
+
+[logger_access]
+handlers=accesslog
+qualname=access
+
+[logger_django_auth_ldap]
+handlers=accesslog
+qualname=django_auth_ldap
+
+[logger_kazoo_client]
+level=INFO
+handlers=errorlog
+qualname=kazoo.client
+
+[logger_djangosaml2]
+level=INFO
+handlers=errorlog
+qualname=djangosaml2
+
+[logger_requests_packages_urllib3_connectionpool]
+level=DEBUG
+handlers=errorlog
+qualname=requests.packages.urllib3.connectionpool
+
+[logger_django_db]
+level=DEBUG
+handlers=logfile
+propogate=False
+qualname=django.db.backends
+
+[logger_boto]
+level=ERROR
+handlers=errorlog
+qualname=boto
+
+[handler_stderr]
+class=StreamHandler
+formatter=default
+level=DEBUG
+args=(sys.stderr,)
+
+[handler_accesslog]
+class=handlers.RotatingFileHandler
+level=INFO
+propagate=True
+formatter=access
+args=('%LOG_DIR%/access.log', 'a', 1000000, 3)
+
+[handler_errorlog]
+class=handlers.RotatingFileHandler
+level=ERROR
+formatter=default
+args=('%LOG_DIR%/error.log', 'a', 1000000, 3)
+
+[handler_logfile]
+class=handlers.RotatingFileHandler
+# Choices are DEBUG, INFO, WARNING, ERROR, CRITICAL
+level=DEBUG
+formatter=default
+args=('%LOG_DIR%/%PROC_NAME%.log', 'a', 1000000, 3)
+
+[formatter_default]
+class=desktop.log.formatter.Formatter
+format=[%(asctime)s] %(module)-12s %(levelname)-8s %(message)s
+datefmt=%d/%b/%Y %H:%M:%S %z
+
+[formatter_access]
+class=desktop.log.formatter.Formatter
+format=[%(asctime)s] %(levelname)-8s %(message)s
+datefmt=%d/%b/%Y %H:%M:%S %z
+
+########################################
+# A summary of loggers, handlers and formatters
+########################################
+
+[loggers]
+keys=root,access,django_auth_ldap,kazoo_client,requests_packages_urllib3_connectionpool,djangosaml2,django_db,boto
+
+[handlers]
+keys=stderr,logfile,accesslog,errorlog
+
+[formatters]
+keys=default,access

+ 32 - 33
desktop/core/src/desktop/log/__init__.py

@@ -61,7 +61,8 @@ def _read_log_conf(proc_name, log_dir):
     elif match.group(0) == '%PROC_NAME%':
       return proc_name
 
-  log_conf = get_desktop_root('conf', 'log.conf')
+  log_conf_file = os.getenv("DESKTOP_LOG_CONF_FILE", 'log.conf')
+  log_conf = get_desktop_root('conf', log_conf_file)
 
   if not os.path.isfile(log_conf):
     return None
@@ -174,38 +175,36 @@ def basic_logging(proc_name, log_dir=None):
   if env_debug:
     env_loglevel = 'DEBUG'
 
-  if env_loglevel:
-    try:
-      lvl = getattr(logging, env_loglevel.upper())
-    except AttributeError:
-      raise Exception("Invalid log level in DESKTOP_LOGLEVEL: %s" % (env_loglevel,))
-
-    # Set the StreamHandler to the level (create one if necessary)
-    handler = _find_console_stream_handler(root_logger)
-    if not handler:
-      handler = logging.StreamHandler()
-      handler.setFormatter(logging.Formatter(LOG_FORMAT, DATE_FORMAT))
-      root_logger.addHandler(handler)
-    if handler:
-      handler.setLevel(lvl)
-
-    # Set all loggers but error.log to the same logging level
-    for h in root_logger.__dict__['handlers']:
-      if isinstance(h, (FileHandler, RotatingFileHandler)):
-        if os.path.basename(h.baseFilename) != 'error.log':
-          h.setLevel(lvl)
-
-    # Set all loggers but error.log to the same logging level
-    for h in root_logger.__dict__['handlers']:
-      if isinstance(h, (SocketHandler)) and h.level != 40:
-        h.setLevel(lvl)
-
-    from desktop.conf import DATABASE_LOGGING
-    if hasattr(DATABASE_LOGGING, 'get') and not DATABASE_LOGGING.get():
-      def disable_database_logging():
-        logger = logging.getLogger()
-        logger.manager.loggerDict['django.db.backends'].level = 20 # INFO level
-      disable_database_logging()
+  # In Python 3, function setLevel will call clear cache in the root logger
+  if not env_loglevel:
+    env_loglevel = 'INFO'
+
+  try:
+    lvl = getattr(logging, env_loglevel.upper())
+  except AttributeError:
+    raise Exception("Invalid log level in DESKTOP_LOGLEVEL: %s" % (env_loglevel,))
+
+  # Set the StreamHandler to the level (create one if necessary)
+  handler = _find_console_stream_handler(root_logger)
+  if not handler:
+    handler = logging.StreamHandler()
+    handler.setFormatter(logging.Formatter(LOG_FORMAT, DATE_FORMAT))
+    root_logger.addHandler(handler)
+  if handler:
+    handler.setLevel(lvl)
+
+  # Set all loggers but error.log to the same logging level
+  for h in root_logger.__dict__['handlers']:
+    if ((isinstance(h, (SocketHandler)) and h.level != 40) or
+        (isinstance(h, (FileHandler, RotatingFileHandler)) and os.path.basename(h.baseFilename) != 'error.log')):
+      h.setLevel(lvl)
+
+  from desktop.conf import DATABASE_LOGGING
+  if hasattr(DATABASE_LOGGING, 'get') and not DATABASE_LOGGING.get():
+    def disable_database_logging():
+      logger = logging.getLogger()
+      logger.manager.loggerDict['django.db.backends'].level = 20 # INFO level
+    disable_database_logging()
 
 def fancy_logging():
   """Configure logging into a buffer for /logs endpoint."""