Skip to content

Commit

Permalink
use coloredlogs library because it detects tty and will not use color…
Browse files Browse the repository at this point in the history
…s in syslog
  • Loading branch information
iceboy233 committed Oct 29, 2017
1 parent 018da18 commit dbfe4b4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 56 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ accept
aioamqp
git+https://github.com/LukeXuan/aiosmtplib # custom aiosmtlib with ssl support
pytz
colorlog
coloredlogs
httpagentparser
geoip2
GitPython
Expand Down
36 changes: 5 additions & 31 deletions vj4/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import atexit
import coloredlogs
import logging
import logging.config
import os
import signal
import socket
Expand All @@ -13,41 +13,15 @@

options.define('listen', default='http://127.0.0.1:8888', help='Server listening address.')
options.define('prefork', default=1, help='Number of prefork workers.')
options.define('log_format',
default=('%(log_color)s[%(levelname).1s '
'%(asctime)s %(module)s:%(lineno)d]%(reset)s %(message)s'),
help='Log format.')

_logger = logging.getLogger(__name__)


def main():
logging.config.dictConfig({
'version': 1,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'colored',
},
},
'formatters': {
'colored': {
'()': 'colorlog.ColoredFormatter',
'format': options.log_format,
'datefmt': '%y%m%d %H:%M:%S'
}
},
'root': {
'level': 'DEBUG' if options.debug else 'INFO',
'handlers': ['console'],
},
'loggers': {
'sockjs': {
'level': 'WARNING',
},
},
'disable_existing_loggers': False,
})
coloredlogs.install(level=logging.DEBUG if options.debug else logging.INFO,
fmt='[%(levelname).1s %(asctime)s %(module)s:%(lineno)d] %(message)s',
datefmt='%y%m%d %H:%M:%S')
logging.getLogger('sockjs').setLevel(logging.WARNING)
url = urllib.parse.urlparse(options.listen)
if url.scheme == 'http':
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Expand Down
27 changes: 3 additions & 24 deletions vj4/util/argmethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
python3.5 -m vj4.model.user --db-name=prod get -1
"""
import collections
import logging
import logging.config

from vj4 import db
from vj4.util import options
Expand All @@ -26,32 +24,13 @@ def wrap(method):


def invoke_by_args():
logging.config.dictConfig({
'version': 1,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'colored',
},
},
'formatters': {
'colored': {
'()': 'colorlog.ColoredFormatter',
'format': '%(log_color)s[%(levelname).1s '
'%(asctime)s %(module)s:%(lineno)d]%(reset)s %(message)s',
'datefmt': '%y%m%d %H:%M:%S'
}
},
'root': {
'level': 'INFO',
'handlers': ['console'],
},
'disable_existing_loggers': False,
})
import argparse
import asyncio
import coloredlogs
import inspect
import pprint
coloredlogs.install(fmt='[%(levelname).1s %(asctime)s %(module)s:%(lineno)d] %(message)s',
datefmt='%y%m%d %H:%M:%S')
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='')
for name, method in _methods.items():
Expand Down

0 comments on commit dbfe4b4

Please sign in to comment.