golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.84k stars 17.65k forks source link

proposal: log/slog: add multiple handlers support for logger #65954

Open lxl-renren opened 8 months ago

lxl-renren commented 8 months ago

Proposal Details

Currently slog package a logger only support one handler. For most language like python, A log module should be sent to multiple targets for output simultaneously for a logger. This feature is necessary, I propose adding multiple handlers support for logger. cc @golang/proposal-review

seankhliao commented 8 months ago

it's unclear why this needs to be in the standard library when it can accomplished as a handler. as the first example I found: https://github.com/samber/slog-multi

lxl-renren commented 8 months ago

I agree other third-party library like "slog-multi" accomplished this functionality. Although third-party solutions exist, standardizing this functionality offers several benefits:

Using third-party library like "slog-multi" may not be maintained consistently, potentially leading to compatibility issues in the future.

ianlancetaylor commented 8 months ago

CC @jba

jba commented 8 months ago

@lxl-renren While I agree with you about the values of standardization, I don't think this particular feature warrants it. It is easy enough to write and not needed often enough to justify the added maintenance burden to the Go team.

However, that is just my opinion, and we can and should let the proposal process take its course.

fishjam commented 2 months ago

I want this feature too. As I know, lots of project will save logs to multi target , example:

current slog can not support such case.

alireza-fa commented 4 weeks ago

If we need to log in two different outputs, we can use io.Multiwriter logger = slog.New( slog.NewJSONHandler(io.MultiWriter(fileWriter, os.Stdout), &slog.HandlerOptions{}), ) If we need to log in two different output with different values, example:

we can warp slog in a custom Logger and handling this sutiations.