Comments (4)
Yes, I just checked, when I change the names to longer ones, then the grafana properly reads them. But the only problem now is that I am losing the colors :( :
slog.SetDefault(slog.New(
tint.NewHandler(os.Stderr, &tint.Options{
AddSource: true,
Level: log,
TimeFormat: time.Kitchen,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.LevelKey {
level := a.Value.Any().(slog.Level)
switch {
case level == slog.LevelError:
a.Value = slog.StringValue("ERROR")
case level == slog.LevelWarn:
a.Value = slog.StringValue("WARN")
case level == slog.LevelInfo:
a.Value = slog.StringValue("INFO")
case level == slog.LevelDebug:
a.Value = slog.StringValue("DEBUG")
default:
a.Value = slog.StringValue("UNKNOWN")
}
}
return a
},
}),
))
from tint.
I played with your code, and managed to set up names with colors, and now everything works perfectly!
const (
ansiReset = "\033[0m"
ansiFaint = "\033[2m"
ansiResetFaint = "\033[22m"
ansiBrightRed = "\033[91m"
ansiBrightGreen = "\033[92m"
ansiBrightYellow = "\033[93m"
ansiBrightRedFaint = "\033[91;2m"
)
// InitLogger initializes the logger
func InitLogger() {
var log slog.Level
if LOG_LEVEL == "info" {
log = slog.LevelInfo
} else {
log = slog.LevelDebug
}
slog.SetDefault(slog.New(
tint.NewHandler(os.Stderr, &tint.Options{
AddSource: true,
Level: log,
TimeFormat: time.Kitchen,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.LevelKey {
level := a.Value.Any().(slog.Level)
switch {
case level == slog.LevelError:
a.Value = slog.StringValue(ansiBrightRed + "ERROR" + ansiReset)
case level == slog.LevelWarn:
a.Value = slog.StringValue(ansiBrightYellow + "WARN" + ansiReset)
case level == slog.LevelInfo:
a.Value = slog.StringValue(ansiBrightGreen + "INFO" + ansiReset)
case level == slog.LevelDebug:
a.Value = slog.StringValue(ansiBrightRedFaint + "DEBUG" + ansiReset)
default:
a.Value = slog.StringValue("UNKNOWN")
}
}
return a
},
}),
))
}
from tint.
hi @mpiorowski, I am glad you found a solution. this pkg needs a better way to customize log levels with coloring (#61). in the meantime your solution is the way to go. personally I always use the native json handler for production environments.
from tint.
@lmittmann but the colors are niceeeee! :D btw amazing jon with lib, thx for your effort
from tint.
Related Issues (20)
- feat request: colorize the message HOT 2
- bug: nested group name was wrong in `Err` attribute HOT 2
- Sorting attrs? HOT 3
- isatty no longer necessary HOT 1
- Is it possible to add a custom time location? HOT 1
- Still colorize level when replaceAttr exist HOT 5
- Fields names are not printed for C structures HOT 4
- Wrong colors somehow HOT 1
- Log files HOT 2
- Colorized attribute HOT 2
- Test TestReplaceAttr fails sometimes HOT 1
- Custom log level names with color HOT 3
- TimeFormat can lead to misaligned output HOT 2
- Log level change HOT 1
- Having an option to keep built-in attributes in the log HOT 2
- Enhancement for source attribute HOT 1
- Adding a new log level with a new color HOT 2
- err/error field is not colored red HOT 3
- Disable time HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tint.