Skip to content

Commit

Permalink
colored logging messages added
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Simkovic committed Jun 27, 2017
1 parent 0953b29 commit 330469c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 11 deletions.
26 changes: 25 additions & 1 deletion conkit/command_line/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
__version__ = "0.1"

import logging
import os


def setup_logging(level='info', logfile=None):
Expand All @@ -54,6 +55,28 @@ def setup_logging(level='info', logfile=None):
Instance of a :obj:`logger <logging.Logger>`
"""

class ColorFormatter(logging.Formatter):
"""Formatter to color console logging output"""

# ANSI foreground color codes
colors = {
logging.DEBUG: 34, # blue
logging.INFO: 0 , # reset to default
logging.WARNING: 33, # yellow
logging.ERROR: 31, # red
logging.CRITICAL: 31, # red
}

def format(self, record):
if record.exc_info is None:
# REM: get the ANSI FG color code
color = ColorFormatter.colors[record.levelno]
prefix = '\033[{}m'.format(color)
record.msg = os.linesep.join([prefix + l for l in str(record.msg).splitlines()])

return logging.Formatter.format(self, record)

# Reset any Handlers or Filters already in the logger to start from scratch
# https://stackoverflow.com/a/16966965
map(logging.getLogger().removeHandler, logging.getLogger().handlers[:])
Expand All @@ -70,7 +93,7 @@ def setup_logging(level='info', logfile=None):
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging_levels.get(level, logging.INFO))
ch.setFormatter(logging.Formatter('%(message)s'))
ch.setFormatter(ColorFormatter('%(message)s'))
logging.getLogger().addHandler(ch)

# create file handler which logs even debug messages
Expand All @@ -86,3 +109,4 @@ def setup_logging(level='info', logfile=None):
logging.getLogger().debug('File logger level: %s', logging.NOTSET)

return logging.getLogger()

15 changes: 13 additions & 2 deletions conkit/command_line/conkit_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@
__version__ = "0.1"

import argparse
import sys

import conkit.command_line
import conkit.io

logger = None


def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
Expand All @@ -62,6 +63,7 @@ def main():
args = parser.parse_args()

# Setup the logger
global logger
logger = conkit.command_line.setup_logging(level='info')

# Perform the conversion
Expand All @@ -73,4 +75,13 @@ def main():


if __name__ == "__main__":
sys.exit(main())
import sys
import traceback
try:
main()
sys.exit(0)
except:
msg = "".join(traceback.format_exception(*sys.exc_info()))
logger.critical(msg)
sys.exit(1)

15 changes: 13 additions & 2 deletions conkit/command_line/conkit_msatool.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@
__version__ = "0.1"

import argparse
import sys

import conkit.command_line
import conkit.io
import conkit.plot

logger = None


def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
Expand All @@ -56,6 +57,7 @@ def main():
args = parser.parse_args()

# Setup the logger
global logger
logger = conkit.command_line.setup_logging(level='info')

# Compute all the data
Expand All @@ -72,4 +74,13 @@ def main():


if __name__ == "__main__":
sys.exit(main())
import sys
import traceback
try:
main()
sys.exit(0)
except:
msg = "".join(traceback.format_exception(*sys.exc_info()))
logger.critical(msg)
sys.exit(1)

15 changes: 13 additions & 2 deletions conkit/command_line/conkit_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@

import argparse
import inspect
import sys

import conkit.command_line
import conkit.io
import conkit.plot

logger = None

# Note:
# New subparsers can be added automatically as long as the convention is followed.
Expand Down Expand Up @@ -246,6 +247,7 @@ def main():
args = parser.parse_args()

# Setup the logger
global logger
logger = conkit.command_line.setup_logging(level='info')

if args.which == 'contact_map':
Expand Down Expand Up @@ -397,4 +399,13 @@ def altloc_remove(cmap):


if __name__ == "__main__":
sys.exit(main())
import sys
import traceback
try:
main()
sys.exit(0)
except:
msg = "".join(traceback.format_exception(*sys.exc_info()))
logger.critical(msg)
sys.exit(1)

16 changes: 14 additions & 2 deletions conkit/command_line/conkit_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@
__version__ = "0.1"

import argparse
import sys

import conkit.command_line
import conkit.io

logger = None


def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
Expand All @@ -64,6 +65,7 @@ def main():
args = parser.parse_args()

# Setup the logger
global logger
logger = conkit.command_line.setup_logging(level='info')

# Compute all the data
Expand Down Expand Up @@ -93,5 +95,15 @@ def main():

return


if __name__ == "__main__":
sys.exit(main())
import sys
import traceback
try:
main()
sys.exit(0)
except:
msg = "".join(traceback.format_exception(*sys.exc_info()))
logger.critical(msg)
sys.exit(1)

15 changes: 13 additions & 2 deletions conkit/command_line/conkit_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@

import argparse
import os
import sys
import time

import conkit.applications
import conkit.command_line
import conkit.io
import conkit.plot

logger = None


def add_default_args(parser):
"""Define default arguments"""
Expand Down Expand Up @@ -106,6 +107,7 @@ def main(argl=None):
args = parser.parse_args(argl)

# Setup the logger
global logger
logger = conkit.command_line.setup_logging(level='info')

logger.info('Prefix: %s', args.prefix)
Expand Down Expand Up @@ -224,4 +226,13 @@ def main(argl=None):


if __name__ == "__main__":
sys.exit(main())
import sys
import traceback
try:
main()
sys.exit(0)
except:
msg = "".join(traceback.format_exception(*sys.exc_info()))
logger.critical(msg)
sys.exit(1)

0 comments on commit 330469c

Please sign in to comment.