Skip to content

Instantly share code, notes, and snippets.

@jmoiron
Last active December 14, 2015 19:29
Show Gist options
  • Save jmoiron/5137052 to your computer and use it in GitHub Desktop.
Save jmoiron/5137052 to your computer and use it in GitHub Desktop.
very simple console color escapes for python & go
const (
white = iota + 89
black
red
green
yellow
blue
purple
)
func color(s string, color int, bold bool) string {
b := "01;"
if !bold {
b = ""
}
return fmt.Sprintf("\033[%s%dm%s\033[0m", b, color, s)
}
white,black,red,green,yellow,blue,purple = range(89,96)
def color(string, color=green, bold=False):
"""Usage: color("foo", red, bold=True)"""
return '\033[%s%sm' % ('01;' if bold else '', color) + str(string) + '\033[0m'
"""This logging config will give you very simply formatted logging output
with color-coded levels."""
import logger
white,black,red,green,yellow,blue,purple = range(89,96)
def color(string, color=green, bold=False):
"""Usage: color("foo", red, bold=True)"""
return '\033[%s%sm' % ('01;' if bold else '', color) + str(string) + '\033[0m'
class Fmt(logging.Formatter):
def format(self, record):
record.levelname = {
'DEBUG': color('DEBUG', white, True),
'INFO': color('INFO', green, True),
'WARNING': color('WARN', yellow, True),
'ERROR': color('ERROR', red, True),
}.get(record.levelname, record.levelname)
return super(Fmt, self).format(record)
formatter = Fmt('%(asctime)s %(levelname)18s: %(message)s')
logging.basicConfig(level=logging.DEBUG)
logging.getLogger().handlers[0].setFormatter(formatter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment