Last active
December 14, 2015 19:29
-
-
Save jmoiron/5137052 to your computer and use it in GitHub Desktop.
very simple console color escapes for python & go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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