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

Add support for logging to multiple locations simultaneously #33

Open
atc0005 opened this issue Sep 30, 2019 · 0 comments
Open

Add support for logging to multiple locations simultaneously #33

atc0005 opened this issue Sep 30, 2019 · 0 comments
Labels
config enhancement New feature or request logging
Milestone

Comments

@atc0005
Copy link
Owner

atc0005 commented Sep 30, 2019

This is handled now via syslog + whatever output is chosen (log file, or one of stdout/stderr), but if using an io.MultiWriter the user could choose to log to ALL chosen outputs simultaneously.

Example:

package logging

import (
    "io"
    logging "log"
    "os"

    "github.com/Sirupsen/logrus"
)

var (
    log *logrus.Logger
)

func init() {
    f, err := os.OpenFile("logs/application.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)

    if err != nil {
        logging.Fatalf("error opening file: %v", err)
    }

    log = logrus.New()

    //log.Formatter = &logrus.JSONFormatter{}

    log.SetReportCaller(true)

    mw := io.MultiWriter(os.Stdout, f)
    log.SetOutput(mw)
}

// Info ...
func Info(format string, v ...interface{}) {
    log.Infof(format, v...)
}

// Warn ...
func Warn(format string, v ...interface{}) {
    log.Warnf(format, v...)
}

// Error ...
func Error(format string, v ...interface{}) {
    log.Errorf(format, v...)
}

var (

    // ConfigError ...
    ConfigError = "%v type=config.error"

    // HTTPError ...
    HTTPError = "%v type=http.error"

    // HTTPWarn ...
    HTTPWarn = "%v type=http.warn"

    // HTTPInfo ...
    HTTPInfo = "%v type=http.info"
)
package main

import (

    log "logging"
)

func main() {
    log.Error(log.ConfigError, "Testing the error")
}

Reference:

https://stackoverflow.com/a/55989885/903870

@atc0005 atc0005 added config enhancement New feature or request logging labels Sep 30, 2019
@atc0005 atc0005 added this to the Future milestone Sep 30, 2019
@atc0005 atc0005 changed the title Consider adding support for logging to multiple locations simultaneously Add support for logging to multiple locations simultaneously Sep 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
config enhancement New feature or request logging
Projects
None yet
Development

No branches or pull requests

1 participant