Code Monkey home page Code Monkey logo

Comments (4)

mpiorowski avatar mpiorowski commented on August 21, 2024

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.

mpiorowski avatar mpiorowski commented on August 21, 2024

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

image

from tint.

lmittmann avatar lmittmann commented on August 21, 2024

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.

mpiorowski avatar mpiorowski commented on August 21, 2024

@lmittmann but the colors are niceeeee! :D btw amazing jon with lib, thx for your effort

from tint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.