mirror of
https://github.com/GNS3/gns3-server
synced 2024-12-25 16:28:11 +00:00
Colored logs
This commit is contained in:
parent
db41076ce5
commit
4488cc3960
@ -22,6 +22,7 @@ import sys
|
|||||||
import locale
|
import locale
|
||||||
|
|
||||||
from gns3server.server import Server
|
from gns3server.server import Server
|
||||||
|
from gns3server.web.logger import init_logger
|
||||||
from gns3server.version import __version__
|
from gns3server.version import __version__
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
@ -85,12 +86,7 @@ def main():
|
|||||||
# user_log.addHandler(stream_handler)
|
# user_log.addHandler(stream_handler)
|
||||||
# user_log.propagate = False
|
# user_log.propagate = False
|
||||||
# END OLD LOG CODE
|
# END OLD LOG CODE
|
||||||
root_log = logging.getLogger()
|
user_log = init_logger(logging.DEBUG, quiet=False)
|
||||||
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
|
|
||||||
# FIXME END Temporary
|
# FIXME END Temporary
|
||||||
|
|
||||||
user_log.info("GNS3 server version {}".format(__version__))
|
user_log.info("GNS3 server version {}".format(__version__))
|
||||||
@ -111,7 +107,7 @@ def main():
|
|||||||
try:
|
try:
|
||||||
os.getcwd()
|
os.getcwd()
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
log.critical("the current working directory doesn't exist")
|
log.critical("The current working directory doesn't exist")
|
||||||
return
|
return
|
||||||
|
|
||||||
# TODO: Renable console_bind_to_any when we will have command line parsing
|
# TODO: Renable console_bind_to_any when we will have command line parsing
|
||||||
|
@ -84,7 +84,7 @@ class Server:
|
|||||||
def _signal_handling(self):
|
def _signal_handling(self):
|
||||||
|
|
||||||
def signal_handler(signame):
|
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()
|
self._stop_application()
|
||||||
|
|
||||||
signals = ["SIGTERM", "SIGINT"]
|
signals = ["SIGTERM", "SIGINT"]
|
||||||
@ -105,7 +105,7 @@ class Server:
|
|||||||
|
|
||||||
def reload():
|
def reload():
|
||||||
|
|
||||||
log.info("reloading")
|
log.info("Reloading")
|
||||||
self._stop_application()
|
self._stop_application()
|
||||||
os.execv(sys.executable, [sys.executable] + sys.argv)
|
os.execv(sys.executable, [sys.executable] + sys.argv)
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ class Server:
|
|||||||
path = path[:-1]
|
path = path[:-1]
|
||||||
modified = os.stat(path).st_mtime
|
modified = os.stat(path).st_mtime
|
||||||
if modified > self._start_time:
|
if modified > self._start_time:
|
||||||
log.debug("file {} has been modified".format(path))
|
log.debug("File {} has been modified".format(path))
|
||||||
reload()
|
reload()
|
||||||
self._loop.call_later(1, self._reload_hook)
|
self._loop.call_later(1, self._reload_hook)
|
||||||
|
|
||||||
@ -137,14 +137,14 @@ class Server:
|
|||||||
self._loop = asyncio.get_event_loop()
|
self._loop = asyncio.get_event_loop()
|
||||||
app = aiohttp.web.Application()
|
app = aiohttp.web.Application()
|
||||||
for method, route, handler in Route.get_routes():
|
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)
|
app.router.add_route(method, route, handler)
|
||||||
for module in MODULES:
|
for module in MODULES:
|
||||||
log.debug("loading module {}".format(module.__name__))
|
log.debug("Loading module {}".format(module.__name__))
|
||||||
m = module.instance()
|
m = module.instance()
|
||||||
m.port_manager = self._port_manager
|
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._loop.run_until_complete(self._run_application(app))
|
||||||
self._signal_handling()
|
self._signal_handling()
|
||||||
|
|
||||||
|
86
gns3server/web/logger.py
Normal file
86
gns3server/web/logger.py
Normal 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')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user