Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom log level names with color #61

Open
nasdf opened this issue Apr 22, 2024 · 3 comments
Open

Custom log level names with color #61

nasdf opened this issue Apr 22, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@nasdf
Copy link

nasdf commented Apr 22, 2024

It'd be great to be able to customize the log level names and keep the colors.

I think simple package level variables would work for most use cases.

var (
  InfoLevelName  = "INF"
  ErrorLevelName = "ERR"
)

Happy to submit a PR if you agree with the feature.

@lmittmann
Copy link
Owner

This is a popular request. You can currently customize levels via the ReplaceAttr option. But I agree that it is not very handy, since you need to manage colors on your own. I am not sure how the API could look like yet, but modifying global variables is not the way.

@iamsumit
Copy link

Raised a PR to provide this feature.

@mpiorowski
Copy link

mpiorowski commented Jul 24, 2024

if anyone will need this, thats how i manage to do it:

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
			},
		}),
	))
}

synfinatic added a commit to synfinatic/tint that referenced this issue Aug 1, 2024
Users via the `LevelColorsMap` can now map their (custom)
slog.Level => name and color.

Fixes: lmittmann#61
synfinatic added a commit to synfinatic/tint that referenced this issue Aug 1, 2024
Users via the `LevelColorsMap` can now map their (custom)
slog.Level => name and color.

Fixes: lmittmann#61
synfinatic added a commit to synfinatic/tint that referenced this issue Aug 1, 2024
Users via the `LevelColorsMap` can now map their (custom)
slog.Level => name and color.

Fixes: lmittmann#61
synfinatic added a commit to synfinatic/tint that referenced this issue Aug 1, 2024
Users via the `LevelColorsMap` can now map their (custom)
slog.Level => name and color.

Fixes: lmittmann#61
synfinatic added a commit to synfinatic/tint that referenced this issue Aug 1, 2024
Users via the `LevelColorsMap` can now map their (custom)
slog.Level => name and color.

Fixes: lmittmann#61
synfinatic added a commit to synfinatic/tint that referenced this issue Aug 1, 2024
Users via the `LevelColorsMap` can now map their (custom)
slog.Level => name and color.

Fixes: lmittmann#61
synfinatic added a commit to synfinatic/tint that referenced this issue Aug 1, 2024
Users via the `LevelColorsMap` can now map their (custom)
slog.Level => name and color.

Fixes: lmittmann#61
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
4 participants