HiveBrain v1.2.0
Get Started
← Back to all entries
patterngoMinor

Deferred log file close

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fileclosedeferredlog

Problem

My code works in that it compiles, and when executed writes out a log file using a buffered writer.

But I wonder whether:

  • I am correct in assuming that this actually winds up deferring a .Close that is always called



  • a package variable is the idiomatic way of doing this in Go



I realise that I could just have createLogger return the file pointer, and defer on that ... but that seems even weirder, to have createLogger set a package variable on log, then return the file pointer to be treated as a local ...

I'm still struggling with an idiom here.

package main

import (
    "bufio"
    "log"
    "os"
)

var logFile *os.File

func main() {
    createLogger()
    defer logFile.Close()

    // do some stuff
}

func createLogger() {
    logFile, err := os.OpenFile("log/app.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
    if err != nil {
        log.Fatalf("error opening file: %v", err)
        os.Exit(-1)
    }
    log.SetOutput(bufio.NewWriter(logFile))
}

Solution

Yes, on both counts (although putting it in the main package is not).

Typically I would return a logFile from createLogger (calling it newLogger is idiomatic), and defer closing the result.

If you're building a logger to work across your whole app, I'd look at how the standard library does it.

Essentially, create a new package my_app_logger with an initializer which sets up the logger. That way, every package that includes my_app_logger gets the same logger instance.

Context

StackExchange Code Review Q#59692, answer score: 4

Revisions (0)

No revisions yet.