Last active
March 12, 2024 05:05
-
-
Save podhmo/5a14bd262668f5238744a352f0af4800 to your computer and use it in GitHub Desktop.
This file contains 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
import logging | |
logger = logging.getLogger(__name__) | |
mapping = { | |
"TRACE": "[ trace ]", | |
"DEBUG": "[ \x1b[0;36mdebug\x1b[0m ]", | |
"INFO": "[ \x1b[0;32minfo\x1b[0m ]", | |
"WARNING": "[ \x1b[0;33mwarn\x1b[0m ]", | |
"WARN": "[ \x1b[0;33mwarn\x1b[0m ]", | |
"ERROR": "\x1b[0;31m[ error ]\x1b[0m", | |
"ALERT": "\x1b[0;37;41m[ alert ]\x1b[0m", | |
"CRITICAL": "\x1b[0;37;41m[ alert ]\x1b[0m", | |
} | |
class ColorfulHandler(logging.StreamHandler): | |
def emit(self, record: logging.LogRecord) -> None: | |
record.levelname = mapping[record.levelname] | |
super().emit(record) | |
logging.basicConfig(handlers=[ColorfulHandler()], level=logging.DEBUG) | |
logger.debug("hello") | |
logger.info("hello") | |
logger.warning("hello") | |
logger.error("hello") | |
logger.critical("hello") |
This file contains 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
package main | |
import ( | |
"log" | |
"os" | |
"github.com/comail/colog" | |
) | |
// https://github.com/comail/colog | |
func main() { | |
colog.Register() | |
colog.SetOutput(os.Stdout) | |
colog.ParseFields(true) | |
colog.SetFlags(log.Ldate | log.Lshortfile) | |
log.Print("trace: logging this to stdout") | |
log.Print("debug: logging this to stdout") | |
log.Print("info: logging this to stdout") | |
log.Print("warning: with fields foo=bar") | |
log.Print("error: with fields foo=bar") | |
log.Print("alert: with fields foo=bar") | |
} |
This file contains 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
[ trace ] 2020/03/01 main.go:18: logging this to stdout | |
[ debug ] 2020/03/01 main.go:19: logging this to stdout | |
[ info ] 2020/03/01 main.go:20: logging this to stdout | |
[ warn ] 2020/03/01 main.go:21: with fields foo=bar | |
[ error ] 2020/03/01 main.go:22: with fields foo=bar | |
[ alert ] 2020/03/01 main.go:23: with fields foo=bar |
Author
podhmo
commented
Mar 1, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment