def setlog(): web_logger.setLevel(logging.CRITICAL) access_logger.setLevel(logging.CRITICAL) client_logger.setLevel(logging.CRITICAL) internal_logger.setLevel(logging.CRITICAL) server_logger.setLevel(logging.CRITICAL) ws_logger.setLevel(logging.CRITICAL)
def setup_logging(*, level: Union[str, int], slow_duration: Optional[float] = None): # service log level logging.basicConfig(level=level) # root logging.root.setLevel(level) config_all_loggers() # aiohttp access log-levels access_logger.setLevel(level) # keep mostly quiet noisy loggers quiet_level: int = max( min(logging.root.level + LOG_LEVEL_STEP, logging.CRITICAL), logging.WARNING) logging.getLogger("engineio").setLevel(quiet_level) logging.getLogger("openapi_spec_validator").setLevel(quiet_level) logging.getLogger("sqlalchemy").setLevel(quiet_level) logging.getLogger("sqlalchemy.engine").setLevel(quiet_level) if slow_duration: # NOTE: Every task blocking > AIODEBUG_SLOW_DURATION_SECS secs is considered slow and logged as warning log_slow_callbacks.enable(abs(slow_duration))
def main(args: Optional[List] = None): # parse & config file parser = ArgumentParser(description="Service to manage data webserver in simcore.") setup_parser(parser) config = parse(args, parser) # service log level log_level = getattr(logging, config["main"]["log_level"]) logging.basicConfig(level=log_level) logging.root.setLevel(log_level) # aiohttp access log-levels access_logger.setLevel(log_level) # keep mostly quiet noisy loggers quiet_level = max( min(log_level + LOG_LEVEL_STEP, logging.CRITICAL), logging.WARNING ) logging.getLogger("engineio").setLevel(quiet_level) logging.getLogger("openapi_spec_validator").setLevel(quiet_level) logging.getLogger("sqlalchemy").setLevel(quiet_level) logging.getLogger("sqlalchemy.engine").setLevel(quiet_level) # NOTE: Every task blocking > AIODEBUG_SLOW_DURATION_SECS secs is considered slow and logged as warning slow_duration = float(os.environ.get("AIODEBUG_SLOW_DURATION_SECS", 0.1)) log_slow_callbacks.enable(slow_duration) # run run_service(config)
def __init__(self, namespace, sio, conf, loop=None): self.name = "WebApp" self.conf = conf self.minupdinterval = conf['minupdinterval'] self.sio = sio self.namespace = namespace self.loop = loop self.con_upd_q = asyncio.Queue(loop=self.loop) self.app = web.Application() self.app._set_loop(self.loop) self.sio.attach(self.app) self.app['websockets'] = [] self.app.router.add_get('/config.json', self.config_json) self.libdir = os.path.dirname(os.path.abspath(__file__)) self.working_dir = conf['working_dir'] self.static_dir = self.libdir + '/html/static' self.templates_dir = self.libdir + '/html/templates' self.user_templates_dir = self.working_dir + '/html/templates' self.app.router.add_route('GET', '/', self.route_handler) self.app.router.add_route('GET', '/favicon.ico', self.favicon_handler) self.app.router.add_route('GET', '/ghwh', self.ghwh_handler) self.app.router.add_route('POST', '/ghwh', self.ghwh_handler) self.app.router.add_route('GET', '/{file_name}', self.route_handler) self.app.router.add_static('/static', self.static_dir) self.app.on_cleanup.append(self.web_on_cleanup) self.app.on_shutdown.append(self.web_on_shutdown) self.backlog = 128 self.con_upd_res = None self.zeroconf_info = None self.zeroconf = None aiohttp_jinja2.setup(self.app, loader=jinja2.FileSystemLoader( self.templates_dir)) self.shutdown_timeout = self.conf['shutdown_timeout'] # self.api_password = conf['api_password'] self.ssl_certificate = conf['ssl_certificate'] self.ssl_key = conf['ssl_key'] self.ssl_context = None self.access_log_format = 'XXX %a %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"' self.access_log = access_logger access_logger.setLevel(logging.WARN) # N.B. set in config self.host = conf['host'] self.port = conf['port']
def init_logging(prefix_log='', enable_console_handler=True): # path of the current file # path = os.path.dirname(os.path.abspath(__file__)) # logs relative directory logs_abs_path = 'logs' # logs filename service_file_name = '%sservice.log' % prefix_log access_file_name = '%saccess.log' % prefix_log error_file_name = '%serror.log' % prefix_log # create logs directory if not os.path.exists(logs_abs_path): os.mkdir(logs_abs_path) # setup log formatter log_formatter = logging.Formatter( u'%(levelname)s: %(asctime)s: %(filename)s[LINE:%(lineno)d] %(message)s' ) # service log service_file_handler = logging.FileHandler( os.path.join(logs_abs_path, service_file_name)) service_file_handler.setFormatter(log_formatter) logger.setLevel(logging.DEBUG) logger.addHandler(service_file_handler) # access log access_file_handler = logging.FileHandler( os.path.join(logs_abs_path, access_file_name)) access_file_handler.setFormatter(log_formatter) access_logger.setLevel(logging.INFO) access_logger.addHandler(access_file_handler) # error log error_file_handler = logging.FileHandler( os.path.join(logs_abs_path, error_file_name)) error_file_handler.setFormatter(log_formatter) error_file_handler.setLevel(logging.ERROR) # Logging service messages into console root_logger = logging.getLogger() root_logger.addHandler(error_file_handler) if enable_console_handler: console_handler = logging.StreamHandler() console_handler.setFormatter(log_formatter) console_handler.setLevel(logging.NOTSET) root_logger.addHandler(console_handler)
import json import logging from aiohttp import web from aiohttp.log import access_logger handler = logging.StreamHandler() access_logger.addHandler(handler) access_logger.setLevel(logging.INFO) async def comments_handler(request): if request.method == 'POST': data = await request.post() index = request.app['data'][-1]['id'] + 1 request.app['data'].append(dict(id=index, **data)) body = json.dumps(request.app['data']).encode('utf8') return web.Response(body=body, content_type='application/javascript') app = web.Application() app.router.add_route('*', '/comments', comments_handler) app['data'] = [ {'id': 1, 'author': 'Pete Hunt', 'text': 'This is one comment'}, {'id': 2, 'author': 'Jonny Walker', 'text': 'This is **another** comment'}, ] async def on_prepare(request, response): response.headers['Access-Control-Allow-Origin'] = 'http://localhost:8000' app.on_response_prepare.append(on_prepare)