1

I use logrus logger and I init it inside my main function like following

main.go file content


import "github.com/sirupsen/logrus"

var logger                *logrus.Logger

func init() {

    logger = utils.InitLogs()
}



func main(){

    logger.Info("bla bla")

   } 

And I was able to use it e.g.logger.info(“test”) in the main file, now I don’t want to create the init file in each other file which need to use the logger , I want to use in other project file the same instance which I’ve created in the main.go file How should I do it in golfing ?

Im not asking about sharing it on the same package

2
  • 2
    logger is visible in all source files within the same package. Commented Dec 6, 2017 at 13:37
  • @dev.bmax - thanks but I dont ask on the same package.... Commented Dec 6, 2017 at 15:14

1 Answer 1

4

There are some points here:

Go groups files in package concept for example if you have some .go files in a directory with the package main that means that all files are sharing the package scope, in this case the func init() inits the var logger that is present in the main package.

I prefer to not use package scope variables, but this depends on your implementation https://dave.cheney.net/2017/06/11/go-without-package-scoped-variables

Maybe you wanted to use logger in different package for example github.com/youruser/work

I prefer to pass the object log to the functions or methods you wanted to use it (as parameters or receiver)

package main

import (
    "github.com/user/utils"
    "github.com/user/work"
)

func main() {
    logger := utils.InitLogs()
    work.Do(logger)
}

or using as receiver

package main

import (
    "github.com/user/utils"
    "github.com/user/work"
)

func main() {
    logger := utils.InitLogs()
    w := work.New()
    w.Log = logger
    w.Do()
}

then in Do() you can log using the receiver

in the work package you should add it as filed

type Worker struct {
    Log *logrus.Logger
}

I really hope you can find it useful.

update: An example of work package

package work

import "github.com/sirupsen/logrus"

type Worker struct {
    Log *logrus.Logger
}

func New() *Worker {
    return new(Worker)
}

func (w *Worker) Do() {
    // some work
    // Here you are passing as receiver parameter
    w.Log.Info("bla bla")
}

func AnotherFunc(log *logrus.Logger) {
    // Here you are passing as method parameter
   log.Info("bla bla")
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, so in this case I need to create constructor for module b(from different package ) ? can you also provide the code which I should use in the work package ?
You should create the logger only one, sure I will update my answer
Ok thanks, so just to see if I get it right, Before I call to the module B (from different package) I need to call to the new method and pass the logger and then use the logger inside this module, am I right?
If you want to use it as receiver yes, you also can pass it as parameter, I'm adding another func in my answer
Ohh, So I should pass it in every function ? its not better to get it from the New method and somehow keep it ? and then re-use it ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.