def init_logger(cls, port): formatter = LogFormatter(fmt=cls.__fmt.format(port=port), datefmt="", color=False) access_log_handler = ConcurrentRotatingFileHandler( filename=os.path.join(ServerConfig["log_dir"], "access.log")) access_log_handler.setFormatter(formatter) access_log.addHandler(access_log_handler) server_log_handler = ConcurrentRotatingFileHandler( filename=os.path.join(ServerConfig['log_dir'], 'server.log'), maxBytes=128 * 1024 * 1024, backupCount=5, encoding='utf8') server_log_handler.setFormatter(formatter) gen_log.addHandler(server_log_handler) app_log.addHandler(server_log_handler) access_log.setLevel(logging.INFO) gen_log.setLevel(getattr(logging, ServerConfig['log_level'].upper())) app_log.setLevel(getattr(logging, ServerConfig['log_level'].upper())) access_log.propagate = app_log.propagate = gen_log.propagate = False return
def set_tornado_log(): handler = logging.StreamHandler(sys.stdout) handler.setFormatter(formatter) gen_log.addHandler(handler) gen_log.setLevel(logging.INFO) access_log.addHandler(handler) access_log.setLevel(logging.INFO) app_log.addHandler(handler) access_log.setLevel(logging.INFO)
def runapp(): os.chdir(os.path.dirname(__file__)) config.load('../config/conf.ini') #set log if platform.system() == 'Windows': LOG_PATH = 'log' else: LOG_PATH = '/usr/log' if False == os.path.exists(LOG_PATH): os.mkdir(LOG_PATH) access_log.propagate = False app_log.propagate = False gen_log.propagate = False handler = logging.handlers.RotatingFileHandler(LOG_PATH + '/api.log', maxBytes=1024 * 1024 * 10, backupCount=5) datefmt = '%Y-%m-%d %H:%M:%S' fmt = '%(levelname)-8s%(asctime)s %(name) -4s%(funcName) -12s %(lineno) -5d: %(message)s' formatter = logging.Formatter(datefmt=datefmt, fmt=fmt) handler.setFormatter(formatter) logger = logging.getLogger('API') logger.addHandler(handler) gen_log.addHandler(handler) app_log.addHandler(handler) access_log.addHandler(handler) logger.setLevel(logging.DEBUG) sys.excepthook = handle_exception MeetingApi = v1.app.VcApi(config.configs) logger.info(MeetingApi.db) MeetingApiServer = tornado.httpserver.HTTPServer(MeetingApi) need_connect = True while (need_connect): try: MeetingApiServer.listen(config.configs['local']['port']) need_connect = False except OSError as e: print('Bind Port %s Failed:' % (config.configs['local']['port'])) print(e) time.sleep(1) tornado.ioloop.IOLoop.current().start()
def enable_tornado_log(): """开启 Tornado 内置日志信息 * ``tornado.access``: Per-request logging for Tornado's HTTP servers (and potentially other servers in the future) * ``tornado.application``: Logging of errors from application code (i.e. uncaught exceptions from callbacks) * ``tornado.general``: General-purpose logging, including any errors or warnings from Tornado itself. """ try: access_log.addHandler(filehandler) access_log.setLevel(logger.level) app_log.addHandler(filehandler) app_log.setLevel(logger.level) gen_log.addHandler(filehandler) gen_log.setLevel(logger.level) except Exception: error_msg = traceback.format_exc() logger.warning(f'enable tornado log fail.\t{error_msg}') logger.error(f'enable tornado log fail.')
if __name__ == "__main__": #rp5lk# # See DummyHandler above. # Create a primary postgres database on startup (in dev only) # c = RunCommand(handler=DummyHandler(), tag='init', command="init_primary -1") # c.run() # Set up Tornado logging gen_log.setLevel(DEBUG) # create formatter and add it to the handlers formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch = logging.StreamHandler() ch.setFormatter(formatter) gen_log.addHandler(ch) # add the handlers to the logger # Define a tornado application application = tornado.web.Application([ (r"/pgnode", MainHandler), ]) lib_dir = '/certs' # Listen on 0.0.0.0:8888. We'll handle addr & port mapping. application.listen(8888, "0.0.0.0", ssl_options=dict( certfile=os.path.join(lib_dir, "server.crt"), keyfile=os.path.join(lib_dir, "server.key") )) # "ca_certs": os.path.join(lib_dir, "client.crt"), # "cert_reqs": CERT_REQUIRED
}) fh_access = logging.handlers.TimedRotatingFileHandler( filename=options.ACCESS_LOG, when='D') fh_access.setFormatter(fmt) access_log.addHandler(fh_access) fh_app = logging.handlers.TimedRotatingFileHandler( filename=options.APPLICATION_LOG, when='D') fh_app.setFormatter(fmt) app_log.addHandler(fh_app) fh_gen = logging.handlers.TimedRotatingFileHandler( filename=options.GENERAL_LOG, when='D') fh_gen.setFormatter(fmt) gen_log.addHandler(fh_gen) from urls import url_patterns application = tornado.web.Application( url_patterns, cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__", xsrf_cookies=False, debug=options.debug, ) if __name__ == "__main__": gen_log.info(',[session_id:],server started. port:{0}'.format( options.port))
def setup_loggers(): """ set up tornado loggers with custom format logger has 5 severity levels : D - DEBUG (lowest) I - INFO W - WARNING E - ERROR C - CRITICAL (highest) """ # config logger output in console # logging.basicConfig( level = logging.DEBUG, # format = "%(name)s - %(funcName)s - %(levelname)s : %(message)s" ) # Create a Formatter for formatting the log messages # log_formatter = logging.Formatter('%(name)s -- %(funcName)s - %(levelname)s - %(message)s') openscraper_log_format = '%(color)s::: %(levelname)s %(name)s %(asctime)s ::: %(module)s:%(lineno)d -in- %(funcName)s() :::%(end_color)s \ %(message)s' # datefmt='%y%m%d %H:%M:%S' # style='%' # color=True # colors={40: 1, 10: 4, 20: 2, 30: 3} """ default tornado format for logging : fmt='%(color)s[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d]%(end_color)s %(message)s' datefmt='%y%m%d %H:%M:%S' style='%' color=True colors={40: 1, 10: 4, 20: 2, 30: 3} )) """ # log_formatter = logging.Formatter( fmt=openscraper_log_format ) tornado_log_formatter = LogFormatter(fmt=openscraper_log_format, color=True) enable_pretty_logging() ### Logger as self var # create the Logger # dictConfig(logging_config) # self.log = logging.getLogger(__name__) # self.logger = logging.getLogger() # self.access_log = logging.getLogger("tornado.access") # self.app_log = logging.getLogger("tornado.application") # self.gen_log = logging.getLogger("tornado.general") ### Get root logger root_logger = logging.getLogger() # print root_logger.__dict__ ### Format root_logger stream # parent_logger = app_log.parent # print parent_logger.__dict__ # root_stream_handler = parent_logger.handlers # root_stream_handler[0].setFormatter(tornado_log_formatter) root_logger.handlers[0].setFormatter(tornado_log_formatter) # streamHandler = logging.StreamHandler() # stream=sys.stdout # streamHandler.setFormatter(tornado_log_formatter) # self.gen_log.addHandler(streamHandler) # self.app_log.addHandler(streamHandler) # self.access_log.addHandler(streamHandler) # self.log.setLevel(logging.DEBUG) # Create the Handlers for logging data to log files gen_log_handler = logging.FileHandler('logs/openscraper_general.log') gen_log_handler.setLevel(logging.WARNING) access_log_handler = logging.FileHandler('logs/openscraper_access.log') access_log_handler.setLevel(logging.WARNING) app_log_handler = logging.FileHandler('logs/openscraper_app.log') app_log_handler.setLevel(logging.WARNING) # Add the Formatter to the Handler gen_log_handler.setFormatter(tornado_log_formatter) access_log_handler.setFormatter(tornado_log_formatter) app_log_handler.setFormatter(tornado_log_formatter) # Add the Handler to the Logger gen_log.addHandler(gen_log_handler) access_log.addHandler(access_log_handler) app_log.addHandler(app_log_handler) # test loggers print app_log.info('>>> this is app_log ') gen_log.info('>>> this is gen_log ') access_log.info('>>> this is access-log ') print
import logging from tornado.log import gen_log, app_log, access_log import os log_file_path = os.path.abspath('./data/log/main.log') logging.basicConfig(level=logging.INFO, filename=log_file_path) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') console_handler = logging.StreamHandler() gen_log.addHandler(console_handler) app_log.addHandler(console_handler) access_log.addHandler(console_handler)
def setup_loggers (): """ set up tornado loggers with custom format logger has 5 severity levels : D - DEBUG (lowest) I - INFO W - WARNING E - ERROR C - CRITICAL (highest) """ # config logger output in console # logging.basicConfig( level = logging.DEBUG, # format = "%(name)s - %(funcName)s - %(levelname)s : %(message)s" ) # Create a Formatter for formatting the log messages # log_formatter = logging.Formatter('%(name)s -- %(funcName)s - %(levelname)s - %(message)s') openscraper_log_format = '%(color)s::: %(levelname)s %(name)s %(asctime)s ::: %(module)s:%(lineno)d -in- %(funcName)s() :::%(end_color)s \ %(message)s' # datefmt='%y%m%d %H:%M:%S' # style='%' # color=True # colors={40: 1, 10: 4, 20: 2, 30: 3} """ default tornado format for logging : fmt='%(color)s[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d]%(end_color)s %(message)s' datefmt='%y%m%d %H:%M:%S' style='%' color=True colors={40: 1, 10: 4, 20: 2, 30: 3} )) """ # log_formatter = logging.Formatter( fmt=openscraper_log_format ) tornado_log_formatter = LogFormatter(fmt=openscraper_log_format, color=True) enable_pretty_logging() ### Logger as self var # create the Logger # dictConfig(logging_config) # self.log = logging.getLogger(__name__) # self.logger = logging.getLogger() # self.access_log = logging.getLogger("tornado.access") # self.app_log = logging.getLogger("tornado.application") # self.gen_log = logging.getLogger("tornado.general") ### Get root logger root_logger = logging.getLogger() # print root_logger.__dict__ ### Format root_logger stream # parent_logger = app_log.parent # print parent_logger.__dict__ # root_stream_handler = parent_logger.handlers # root_stream_handler[0].setFormatter(tornado_log_formatter) root_logger.handlers[0].setFormatter(tornado_log_formatter) # streamHandler = logging.StreamHandler() # stream=sys.stdout # streamHandler.setFormatter(tornado_log_formatter) # self.gen_log.addHandler(streamHandler) # self.app_log.addHandler(streamHandler) # self.access_log.addHandler(streamHandler) # self.log.setLevel(logging.DEBUG) # Create the Handlers for logging data to log files gen_log_handler = logging.FileHandler('logs/openscraper_general.log') gen_log_handler.setLevel(logging.WARNING) access_log_handler = logging.FileHandler('logs/openscraper_access.log') access_log_handler.setLevel(logging.WARNING) app_log_handler = logging.FileHandler('logs/openscraper_app.log') app_log_handler.setLevel(logging.WARNING) # Add the Formatter to the Handler gen_log_handler.setFormatter(tornado_log_formatter) access_log_handler.setFormatter(tornado_log_formatter) app_log_handler.setFormatter(tornado_log_formatter) # Add the Handler to the Logger gen_log.addHandler(gen_log_handler) access_log.addHandler(access_log_handler) app_log.addHandler(app_log_handler) # test loggers print() app_log.info('>>> this is app_log ') gen_log.info('>>> this is gen_log ') access_log.info('>>> this is access_log ') print()
if __name__ == "__main__": #rp5lk# # See DummyHandler above. # Create a primary postgres database on startup (in dev only) # c = RunCommand(handler=DummyHandler(), tag='init', command="init_primary -1") # c.run() # Set up Tornado logging gen_log.setLevel(DEBUG) # create formatter and add it to the handlers formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch = logging.StreamHandler() ch.setFormatter(formatter) gen_log.addHandler(ch) # add the handlers to the logger # Define a tornado application application = tornado.web.Application([ (r"/pgnode", MainHandler), ]) lib_dir = '/certs' # Listen on 0.0.0.0:8888. We'll handle addr & port mapping. application.listen(8888, "0.0.0.0", ssl_options=dict( certfile=os.path.join(lib_dir, "server.crt"), keyfile=os.path.join(lib_dir, "server.key") )) # "ca_certs": os.path.join(lib_dir, "client.crt"), # "cert_reqs": CERT_REQUIRED