1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-12 19:38:57 +00:00

Colored logs

This commit is contained in:
Julien Duponchelle 2015-01-20 14:59:19 +01:00
parent db41076ce5
commit 4488cc3960
3 changed files with 95 additions and 13 deletions

View File

@ -22,6 +22,7 @@ import sys
import locale
from gns3server.server import Server
from gns3server.web.logger import init_logger
from gns3server.version import __version__
import logging
@ -85,12 +86,7 @@ def main():
# user_log.addHandler(stream_handler)
# user_log.propagate = False
# END OLD LOG CODE
root_log = logging.getLogger()
root_log.setLevel(logging.DEBUG)
console_log = logging.StreamHandler(sys.stdout)
console_log.setLevel(logging.DEBUG)
root_log.addHandler(console_log)
user_log = root_log
user_log = init_logger(logging.DEBUG, quiet=False)
# FIXME END Temporary
user_log.info("GNS3 server version {}".format(__version__))
@ -111,7 +107,7 @@ def main():
try:
os.getcwd()
except FileNotFoundError:
log.critical("the current working directory doesn't exist")
log.critical("The current working directory doesn't exist")
return
# TODO: Renable console_bind_to_any when we will have command line parsing

View File

@ -84,7 +84,7 @@ class Server:
def _signal_handling(self):
def signal_handler(signame):
log.warning("server has got signal {}, exiting...".format(signame))
log.warning("Server has got signal {}, exiting...".format(signame))
self._stop_application()
signals = ["SIGTERM", "SIGINT"]
@ -105,7 +105,7 @@ class Server:
def reload():
log.info("reloading")
log.info("Reloading")
self._stop_application()
os.execv(sys.executable, [sys.executable] + sys.argv)
@ -124,7 +124,7 @@ class Server:
path = path[:-1]
modified = os.stat(path).st_mtime
if modified > self._start_time:
log.debug("file {} has been modified".format(path))
log.debug("File {} has been modified".format(path))
reload()
self._loop.call_later(1, self._reload_hook)
@ -137,14 +137,14 @@ class Server:
self._loop = asyncio.get_event_loop()
app = aiohttp.web.Application()
for method, route, handler in Route.get_routes():
log.debug("adding route: {} {}".format(method, route))
log.debug("Adding route: {} {}".format(method, route))
app.router.add_route(method, route, handler)
for module in MODULES:
log.debug("loading module {}".format(module.__name__))
log.debug("Loading module {}".format(module.__name__))
m = module.instance()
m.port_manager = self._port_manager
log.info("starting server on {}:{}".format(self._host, self._port))
log.info("Starting server on {}:{}".format(self._host, self._port))
self._loop.run_until_complete(self._run_application(app))
self._signal_handling()

86
gns3server/web/logger.py Normal file
View File

@ -0,0 +1,86 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Provide a pretty logging on console"""
import logging
import sys
class ColouredFormatter(logging.Formatter):
RESET = '\x1B[0m'
RED = '\x1B[31m'
YELLOW = '\x1B[33m'
GREEN = '\x1B[32m'
PINK = '\x1b[35m'
def format(self, record, colour=False):
message = super().format(record)
if not colour:
return message
level_no = record.levelno
if level_no >= logging.CRITICAL:
colour = self.RED
elif level_no >= logging.ERROR:
colour = self.RED
elif level_no >= logging.WARNING:
colour = self.YELLOW
elif level_no >= logging.INFO:
colour = self.GREEN
elif level_no >= logging.DEBUG:
colour = self.PINK
else:
colour = self.RESET
message = '{colour}{message}{reset}'.format(colour=colour, message=message, reset=self.RESET)
return message
class ColouredStreamHandler(logging.StreamHandler):
def format(self, record, colour=False):
if not isinstance(self.formatter, ColouredFormatter):
self.formatter = ColouredFormatter()
return self.formatter.format(record, colour)
def emit(self, record):
stream = self.stream
try:
msg = self.format(record, stream.isatty())
stream.write(msg)
stream.write(self.terminator)
self.flush()
except Exception:
self.handleError(record)
def init_logger(level,quiet=False):
stream_handler = ColouredStreamHandler(sys.stdout)
stream_handler.formatter = ColouredFormatter("{asctime} {levelname:8} {filename}:{lineno} {message}", "%Y-%m-%d %H:%M:%S", "{")
if quiet:
stream_handler.addFilter(logging.Filter(name="user_facing"))
logging.getLogger('user_facing').propagate = False
logging.basicConfig(level=level, handlers=[stream_handler])
return logging.getLogger('user_facing')