FILE / ScuroNeko/SNekLog
examples/main.go
Исходный файл и его история в репозитории.
Golang lint / lint (push) Successful in 24s
- add custom LogLevel constructors and richer color configuration - support ANSI, 256-color, RGB, and text attributes on LogLevel - introduce clearer preferred APIs such as NewLogger and SetName - preserve backward compatibility with deprecated wrappers for v2.0.1 APIs - restore legacy ColorStringBuilder signatures as compatibility wrappers - fix newline separator handling in Println-style output - add tests for color precedence, deprecated aliases, and LogLevel attributes - update GoDoc and bilingual README documentation
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"git.scuroneko.dev/scuroneko/sneklog/v2"
|
|
)
|
|
|
|
func main() {
|
|
httpGet := sneklog.NewLogLevelWithColors(0, "get", sneklog.FgGreen, sneklog.BgNone)
|
|
httpPost := sneklog.NewLogLevelWithColors(0, "post", sneklog.FgBlue, sneklog.BgNone)
|
|
httpDelete := sneklog.NewLogLevel(0, "delete")
|
|
httpDelete.SetBackgroundRGB(128, 0, 0)
|
|
httpDelete.AddAttribute(sneklog.Italic).AddAttribute(sneklog.Bold)
|
|
|
|
logger := sneklog.NewLogger().
|
|
SetName("EXAMPLE").
|
|
SetLevel(sneklog.DEBUG).
|
|
SetJSONPretty(true).
|
|
AddReplacer("SOME_SECRET", "<Secret>")
|
|
|
|
sneklog.INFO.SetBgColor(sneklog.BgBlue).SetFgColor(sneklog.FgWhite)
|
|
|
|
textStdout := logger.CreateTextStdoutWriter()
|
|
jsonStdout := logger.CreateJsonStdoutWriter()
|
|
|
|
formatter := sneklog.NewFormatter().
|
|
SetFormat("[%t] [%L] [%N]: %m (%s)").
|
|
SetTimeStampFormat(sneklog.Kitchen).
|
|
SetTraceBackFormat("%s %f:%n %p")
|
|
|
|
jsonFormatter := sneklog.NewFormatter().
|
|
SetFormat("[%L] [%N] %m").
|
|
SetColorOutput(false).
|
|
SetTimeStampFormat(sneklog.Kitchen)
|
|
|
|
textStdout.SetFormatter(formatter)
|
|
jsonStdout.SetFormatter(jsonFormatter)
|
|
|
|
textFile, err := logger.CreateTextFileWriter("logs/text.log")
|
|
if err != nil {
|
|
_ = logger.Close()
|
|
panic(err)
|
|
}
|
|
|
|
jsonFile, err := logger.CreateJsonFileWriter("logs/json.log")
|
|
if err != nil {
|
|
_ = logger.Close()
|
|
panic(err)
|
|
}
|
|
|
|
var externalBuffer bytes.Buffer
|
|
externalText := logger.CreateTextWriter(&externalBuffer)
|
|
externalJSON := logger.CreateJsonWriter(&externalBuffer)
|
|
|
|
logger.AddWriters(
|
|
textStdout,
|
|
jsonStdout,
|
|
textFile,
|
|
jsonFile,
|
|
externalText,
|
|
externalJSON,
|
|
)
|
|
|
|
logger.Infoln("service started")
|
|
logger.Warnln("cache miss")
|
|
logger.Errorln("request failed")
|
|
logger.Debugln("debug details")
|
|
logger.Infoln("sensitive info, SOME_SECRET")
|
|
logger.Println(httpGet, "this can be logging of http get request/response...")
|
|
logger.Println(httpPost, "...and this can be logging of http post request/response...")
|
|
logger.Println(httpDelete, "...but this logging of http delete request/response with highly customizable level!")
|
|
|
|
if err := logger.Close(); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
s := sneklog.NewColorStringBuilder().
|
|
AddBackgroundRGB(sneklog.NewBgColorRGB(31, 41, 40)).
|
|
AddForegroundRGB(sneklog.NewFgColorRGB(220, 215, 186)).
|
|
AddAttribute(sneklog.Italic).AddAttribute(sneklog.Bold).
|
|
AddText("Some Very very very cool stuff, themed in Kanagawa colors!").String()
|
|
|
|
println(s)
|
|
|
|
//fmt.Println("external buffer contents:")
|
|
//fmt.Println(externalBuffer.String())
|
|
}
|