Skip to content
This repository has been archived by the owner on Aug 12, 2019. It is now read-only.

Add more complex examples #57

Open
ctradu opened this issue Dec 14, 2015 · 2 comments
Open

Add more complex examples #57

ctradu opened this issue Dec 14, 2015 · 2 comments

Comments

@ctradu
Copy link

ctradu commented Dec 14, 2015

Hi,

I am interested in configuring sawmill to log at the same time to

  • os.Stdout
  • a textfile
  • syslog

Using only the provided api docs doesn't help me that much when things go bad.
For example:

var logger = sm.NewLogger()
func InitLogging(logfile string) {
    logf, err := os.Create(logfile)
    if err != nil {
        sm.Critical("unable to open logfile: ", err)
    }
    logfileWriter := bufio.NewWriter(logf)

    fileHandler, err := smw.New(logfileWriter, formatter.CONSOLE_NOCOLOR_FORMAT)
    if err != nil {
       sm.Error("unable to create logfile handler")
    }
    logger.AddHandler("localfile", fileHandler)
    logger.Debug("logger-write some message")
}

As soon as I run this function I lose both logs in that file and on Stdout ...

@phemmer
Copy link
Owner

phemmer commented Dec 16, 2015

As soon as I run this function I lose both logs in that file and on Stdout ...

I think these are actually 3 separate problems.

  • The first is that os.Create() truncates the file. You can use os.OpenFile() to open for writing without truncate. To resolve this on the sawmill side, I could add a helper function to the handler/writer package. Such as writer.NewFileAppender(path string, mode int). Which would open the file for appending, creating it only if it doesn't exist.
  • Then, the bufio.NewWriter() is creating an async write buffer, that doesn't get flushed automatically. So unless an explicit call to logfileWriter.Flush() is made, log entries may be missing. For this, I might be open to adding a Stop() method to the handler interface, this could be called when a handler is removed from the logger (logger is shut down). The handler/writer package would then make sure Close() is called on the underlying writer which would flush the buffer.
  • In regards to losing STDOUT, sm.NewLogger() creates a completely empty logger. It doesn't output to STDOUT or anything. If you call InitStdStreams(), that will set up STDOUT/STDERR on the logger.

Lastly, I can add some more examples which show use cases such as creating a custom logger which writes to multiple destinations.

I'll take a look at these things this weekend (maybe sooner if I have some time).

And thank you for the feedback :-)

@phemmer
Copy link
Owner

phemmer commented Dec 23, 2015

Just an FYI, #58 was merged which adds an append helper.

I have an idea how I'm going to do the close/flush thing, but I want to give it some more thought as it might be a significant design change.

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

No branches or pull requests

2 participants