Skip to content

Instantly share code, notes, and snippets.

@podhmo
Last active March 12, 2024 05:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/5a14bd262668f5238744a352f0af4800 to your computer and use it in GitHub Desktop.
Save podhmo/5a14bd262668f5238744a352f0af4800 to your computer and use it in GitHub Desktop.
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")
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")
}
[ 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
@podhmo
Copy link
Author

podhmo commented Mar 1, 2020

Screenshot 2020-03-01 21 08 35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment