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

No method available to add Fields to existing globalLogger variable #9

Open
jajupreetam opened this issue Aug 29, 2020 · 6 comments
Open

Comments

@jajupreetam
Copy link

jajupreetam commented Aug 29, 2020

HI,
I want to add few values to Fields of the exiting globalLogger, not to the new one created by GetLogger() or func NewLogger(name string) . because I see that no possiblity to again set back newly created *Logger to globalLogger variable.
This is also becasue , slf_core.go always uses globalLogger & not the one I created instance of Logger

func Debug(v ...interface{}) {
...
...
globalLogger.print(DebugLevel, pc[0], nil, v...)
}

Either provide a method to set the newly created Logger to globalLogger or provide new getGlobalLogger() to get handle to globalLogger so that Fields can be added.

somethig like below in slf_core.go :

func GetGlobalLogger() *Logger {
return globalLogger
}

OR

func AddFields(fields Fields) {
globalLogger.BindFields(fields Fields)
}

@sisyphsu
Copy link
Collaborator

sisyphsu commented Aug 30, 2020

The globalLogger is used as default logger, I don't think it is a good idea to modify the default logger that other packages may use.

You should create a customized Logger in your own package:

var log = slog.NewLogger()

// ...

func init() {
    log.BindFields(...) // some global fields
}

func yourFunc() {
    log.Info("......") // log with global fields

    log.WithFields(...).Warn("...") // log with global and transient fields to log
}

@jajupreetam
Copy link
Author

jajupreetam commented Aug 30, 2020

My requirement is to log sessionid which is one of the element in the JWT token claims body which comes with every request. So the fields which i have to add to logger is not available by the time when init() gets executed.

Or can you please let me know how to print sid (sessionid) with every log statement without calling log.WithFields(sid value) everytime.

@sisyphsu
Copy link
Collaborator

Or can you please let me know how to print sid (sessionid) with every log statement without calling log.WithFields(sid value) everytime.

OK, this is a good question.

What you need is Fields for goroutine, like MDC in slf4j, to bind some variables on gorouting/thread, am I correct?

@sisyphsu
Copy link
Collaborator

This is a very useful feature, but slf4go don't support it by now.

I think i could implement it soon.

@jajupreetam
Copy link
Author

jajupreetam commented Aug 31, 2020

Or can you please let me know how to print sid (sessionid) with every log statement without calling log.WithFields(sid value) everytime.

OK, this is a good question.

What you need is Fields for goroutine, like MDC in slf4j, to bind some variables on gorouting/thread, am I correct?

Correct, this is what I need here like MDC in slf4j to add session Ids/any other fields.

@sisyphsu
Copy link
Collaborator

sisyphsu commented Jun 18, 2021

This work has done, in v1.1.0 there has some new functions:

  • SetContextField(key string, value interface{})
  • SetContextFields(fields Fields)
  • ......

There hasn't docs yet, but I believe you will know how to use them.

You can use those functions to add context variables into goroutine's local storage, and those Fields will appear in all log.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants