8

I am writing an application in which i need to record logs in two different files. For Example weblogs.go and debuglogs.go. I tried using the log4go but my requirement is i need the logger to be created in main file and be accessible in sub directory as major of decoding and logging is done in sub file. Can anybody please help with that?

Zubair Hassan
  • 776
  • 6
  • 14
Madhuri Desai
  • 117
  • 1
  • 5
  • https://www.scalyr.com/blog/go-logging, https://github.com/sirupsen/logrus, https://stackoverflow.com/questions/30257622/golang-logrus-how-to-do-a-centralized-configuration – sulabh chaturvedi Feb 22 '19 at 07:52
  • https://awesome-go.com/#logging you can find list of popular logging packages here. – IhtkaS Feb 22 '19 at 08:05
  • 1
    What's stopping you from passing the loggers from main to the things that need them? It's not clear what exactly you need help with. – Peter Feb 22 '19 at 08:57
  • @Peter I have my logger declared in my main function in my parent package. I wanted to access the same logger variable in sub directory. Since you cannot import main, i am not getting a way to make my logger variable accessible in sub package . – Madhuri Desai Feb 22 '19 at 09:06
  • 1
    Loggers are just values. You can pass them down as function arguments or in struct fields, for example. If you really want to use globals, make a new package that *can* be imported and initialize it in main. – Peter Feb 22 '19 at 10:25

2 Answers2

7

Here's one way to do it, using the standard log package:

package main

import (
    "io"
    "log"
    "os"
)

func main() {
    f1, err := os.Create("/tmp/file1")
    if err != nil {
        panic(err)
    }
    defer f1.Close()

    f2, err := os.Create("/tmp/file2")
    if err != nil {
        panic(err)
    }
    defer f2.Close()

    w := io.MultiWriter(os.Stdout, f1, f2)
    logger := log.New(w, "logger", log.LstdFlags)

    myfunc(logger)
}

func myfunc(logger *log.Logger) {
    logger.Print("Hello, log file!!")
}

Notes:

  1. io.MultiWriter is used to combine several writers together. Here, it creates a writer w - a write to w will go to os.Stdout as well as two files
  2. log.New lets us create a new log.Logger object with a custom writer
  3. The log.Logger object can be passed around to functions and used by them to log things
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
1

This is how you can manage logs for debugging and prod envoirments with multi log files:

First, make a type for managing multiple loggers you can make a separate file for this like logging.go

type LOGGER struct {
        debug *log.Logger
        prod  *log.Logger
        .
        .
        .
    }

For getting the LOGGER pointer anywhere in your project the best approach will be to get it through the singleton pattern like

    var lock = &sync.Mutex{}
    var loggers *LOGGER

    func GetLoggerInstance() *LOGGER {
      lock.Lock()
      defer lock.Unlock()

    if loggers == nil {
      fmt.Println("Creating LOGGER instance now.")
      loggers = &LOGGER{} 
    } else {
        fmt.Println("LOGGER instance already created.")
    }

    return loggers
}
    

Now setup logger for debug env in any pkg or directory level better is to setup it at the root level

    logger := GetLoggerInstance()
    f, err := os.OpenFile("path/to/debug/log/file", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
    if err != nil {
        fmt.Println("debug log file not created", err.Error())
    }
    loggers.debug = log.New(f, "[DEBUG]", log.Ldate|log.Ltime|log.Lmicroseconds|log.LUTC)

    loggers.debug.Println("This is debug log message")

With the same approach, you can also create prod or as many as you want.

    logger := GetLoggerInstance()
    f, err = os.OpenFile("path/to/prod/log/file", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
    if err != nil {
        fmt.Println("prod log file not created", err.Error())
    }
    loggers.prod = log.New(f, "[PROD]", log.Ldate|log.Ltime|log.Lmicroseconds|log.LUTC)

    loggers.prod.Println("This is prod log message")

Zubair Hassan
  • 776
  • 6
  • 14