44# Email: wuyue92tree@163.com
55
66
7+ import os
8+ import sys
79import logging
810import logging .config
911import logging .handlers
1012from crwy .exceptions import CrwyException
13+ from crwy .settings .default_settings import TEMPLATE_DIR
1114
1215try :
13- import ConfigParser
16+ import ConfigParser as configparser
1417except ImportError :
15- from configparser import ConfigParser
18+ import configparser
1619
1720DEFAULT_LOGGER_CONF = './conf/logger.conf'
1821
22+ if sys .version_info [0 ] == 2 :
23+ BASE_LOGGER_CONF = os .path .join (
24+ TEMPLATE_DIR , 'project/logger_py2.conf.tmpl' )
25+ else :
26+ BASE_LOGGER_CONF = os .path .join (
27+ TEMPLATE_DIR , 'project/logger_py3.conf.tmpl' )
28+
1929try :
20- logging .config .fileConfig (DEFAULT_LOGGER_CONF )
30+ try :
31+ logging .config .fileConfig (DEFAULT_LOGGER_CONF )
32+ except KeyError :
33+ logging .config .fileConfig (BASE_LOGGER_CONF )
2134except :
2235 pass
2336
@@ -54,7 +67,10 @@ def _install_handlers_custom(cp, formatters, log_path):
5467
5568 if "level" in opts :
5669 level = cp .get (sectname , "level" )
57- h .setLevel (logging ._levelNames [level ])
70+ try :
71+ h .setLevel (logging ._levelNames [level ])
72+ except AttributeError :
73+ h .setLevel (logging ._nameToLevel [level ])
5874 if len (fmt ):
5975 h .setFormatter (formatters [fmt ])
6076 if issubclass (klass , logging .handlers .MemoryHandler ):
@@ -71,7 +87,7 @@ def _install_handlers_custom(cp, formatters, log_path):
7187 return handlers
7288
7389
74- def fileConfigWithLogPath (fname = DEFAULT_LOGGER_CONF ,
90+ def fileConfigWithLogPath (fname = BASE_LOGGER_CONF ,
7591 log_path = None ,
7692 defaults = None ,
7793 disable_existing_loggers = True ):
@@ -81,14 +97,14 @@ def fileConfigWithLogPath(fname=DEFAULT_LOGGER_CONF,
8197 if not log_path :
8298 raise CrwyException ('Please setup <log_path> first!' )
8399
84- cp = ConfigParser .ConfigParser (defaults )
100+ cp = configparser .ConfigParser (defaults )
85101 if hasattr (fname , 'readline' ):
86- cp .readfp (fname )
102+ cp .read_file (fname )
87103 else :
88104 cp .read (fname )
89105 try :
90106 formatters = logging .config ._create_formatters (cp )
91- except ConfigParser .NoSectionError :
107+ except configparser .NoSectionError :
92108 raise CrwyException ('Please make sure fname: "%s" is exist.' % fname )
93109
94110 logging ._acquireLock ()
0 commit comments