cwconfig.py
changeset 180 8bcebdb5f55d
parent 140 478bdd15bc0e
child 369 c8a6edc224bb
equal deleted inserted replaced
179:7bf8207d5d28 180:8bcebdb5f55d
    10 import os
    10 import os
    11 import logging
    11 import logging
    12 from os.path import exists, join, expanduser, abspath, basename
    12 from os.path import exists, join, expanduser, abspath, basename
    13 
    13 
    14 from logilab.common.decorators import cached
    14 from logilab.common.decorators import cached
       
    15 from logilab.common.logging_ext import set_log_methods, init_log
    15 from logilab.common.configuration import (Configuration, Method,
    16 from logilab.common.configuration import (Configuration, Method,
    16                                           ConfigurationMixIn, merge_options)
    17                                           ConfigurationMixIn, merge_options)
    17 
    18 
    18 from cubicweb import CW_SOFTWARE_ROOT, CW_MIGRATION_MAP, ConfigurationError
    19 from cubicweb import CW_SOFTWARE_ROOT, CW_MIGRATION_MAP, ConfigurationError
    19 from cubicweb.toolsutils import env_path, read_config, create_dir
    20 from cubicweb.toolsutils import env_path, read_config, create_dir
   441         self.cls_adjust_sys_path()
   442         self.cls_adjust_sys_path()
   442         
   443         
   443     def init_log(self, logthreshold=None, debug=False, 
   444     def init_log(self, logthreshold=None, debug=False, 
   444                  logfile=None, syslog=False):
   445                  logfile=None, syslog=False):
   445         """init the log service"""
   446         """init the log service"""
   446         if os.environ.get('APYCOT_ROOT'):
   447         if logthreshold is None:
   447             logthreshold = logging.CRITICAL
       
   448             # redirect logs to stdout to avoid apycot output parsing failure
       
   449             handler = logging.StreamHandler(sys.stdout)
       
   450         else:
       
   451             if debug:
   448             if debug:
   452                 if logthreshold is None:
   449                 logthreshold = 'DEBUG'
   453                     logthreshold = logging.DEBUG # LLDEBUG
       
   454                 handler = logging.StreamHandler()
       
   455             elif logfile is None:
       
   456                 if syslog:
       
   457                     from logging import handlers
       
   458                     handler = handlers.SysLogHandler()
       
   459                 else:
       
   460                     handler = logging.StreamHandler()
       
   461             else:
   450             else:
   462                 try:
   451                 logthreshold = self['log-threshold']
   463                     handler = logging.FileHandler(logfile)
   452         init_log(debug, syslog, logthreshold, logfile, self.log_format)
   464                 except IOError:
       
   465                     handler = logging.StreamHandler()
       
   466             if logthreshold is None:
       
   467                 thresholdname = self['log-threshold']
       
   468                 logthreshold = getattr(logging, THRESHOLD_MAP.get(thresholdname,
       
   469                                                                   thresholdname))
       
   470         # configure the root logger
       
   471         logger = logging.getLogger()
       
   472         logger.setLevel(logthreshold)
       
   473         # only addHandler and removeHandler method while I would like a
       
   474         # setHandler method, so do it this way :$
       
   475         logger.handlers = [handler]
       
   476         isatty = hasattr(sys.__stdout__, 'isatty') and sys.__stdout__.isatty()
       
   477         if debug and isatty:
       
   478             from logilab.common.logging_ext import ColorFormatter
       
   479             fmt = ColorFormatter(self.log_format, '%Y-%m-%d %H:%M:%S')
       
   480             def col_fact(record):
       
   481                 if 'XXX' in record.message:
       
   482                     return 'cyan'
       
   483                 if 'kick' in record.message:
       
   484                     return 'red'
       
   485             fmt.colorfilters.append(col_fact)
       
   486         else:
       
   487             fmt = logging.Formatter(self.log_format, '%Y-%m-%d %H:%M:%S')
       
   488         logger.handlers[0].setFormatter(fmt)
       
   489         # configure simpleTal logger
   453         # configure simpleTal logger
   490         logging.getLogger('simpleTAL').setLevel(logging.ERROR)
   454         logging.getLogger('simpleTAL').setLevel(logging.ERROR)
   491 
   455 
   492     def vregistry_path(self):
   456     def vregistry_path(self):
   493         """return a list of files or directories where the registry will look
   457         """return a list of files or directories where the registry will look
   837             create_dir(i18ndir)
   801             create_dir(i18ndir)
   838         sourcedirs = [join(path, 'i18n') for path in self.cubes_path()]
   802         sourcedirs = [join(path, 'i18n') for path in self.cubes_path()]
   839         sourcedirs.append(self.i18n_lib_dir())
   803         sourcedirs.append(self.i18n_lib_dir())
   840         return i18n.compile_i18n_catalogs(sourcedirs, i18ndir, langs)
   804         return i18n.compile_i18n_catalogs(sourcedirs, i18ndir, langs)
   841 
   805 
       
   806 set_log_methods(CubicWebConfiguration, logging.getLogger('cubicweb.configuration'))
   842         
   807         
   843 # alias to get a configuration instance from an application id
   808 # alias to get a configuration instance from an application id
   844 application_configuration = CubicWebConfiguration.config_for        
   809 application_configuration = CubicWebConfiguration.config_for        
   845 
   810 
   846 # map logilab.common.logger thresholds to logging thresholds
       
   847 THRESHOLD_MAP = {'LOG_DEBUG':  'DEBUG',
       
   848                  'LOG_INFO':   'INFO',
       
   849                  'LOG_NOTICE': 'INFO',
       
   850                  'LOG_WARN':   'WARNING',
       
   851                  'LOG_ERR':    'ERROR',
       
   852                  'LOG_CRIT':   'CRITICAL',
       
   853                  }
       
   854 
       
   855 from cubicweb import set_log_methods
       
   856 set_log_methods(CubicWebConfiguration, logging.getLogger('cubicweb.configuration'))