How to do it...

These steps cover the writing and running of your application:

  1. From your Terminal/console application, create a new directory called ~/projects/go-programming-cookbook/chapter4/log and navigate to this directory.
  2. Run the following command:
$ go mod init github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter4/log

You should see a file called go.mod that contains the following:

module github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter4/log    
  1. Copy tests from ~/projects/go-programming-cookbook-original/chapter4/log, or use this as an exercise to write some of your own code!
  2. Create a file called log.go with the following content:
        package log

import (
"bytes"
"fmt"
"log"
)

// Log uses the setup logger
func Log() {
// we'll configure the logger to write
// to a bytes.Buffer
buf := bytes.Buffer{}

// second argument is the prefix last argument is about
// options you combine them with a logical or.
logger := log.New(&buf, "logger: ",
log.Lshortfile|log.Ldate)

logger.Println("test")

logger.SetPrefix("new logger: ")

logger.Printf("you can also add args(%v) and use Fatalln to
log and crash", true)

fmt.Println(buf.String())
}
  1. Create a file called error.go with the following content:
        package log

import "github.com/pkg/errors"
import "log"

// OriginalError returns the error original error
func OriginalError() error {
return errors.New("error occurred")
}

// PassThroughError calls OriginalError and
// forwards the error along after wrapping.
func PassThroughError() error {
err := OriginalError()
// no need to check error
// since this works with nil
return errors.Wrap(err, "in passthrougherror")
}

// FinalDestination deals with the error
// and doesn't forward it
func FinalDestination() {
err := PassThroughError()
if err != nil {
// we log because an unexpected error occurred!
log.Printf("an error occurred: %s\n", err.Error())
return
}
}
  1. Create a new directory named example and navigate to it.
  2. Create a main.go file with the following content:
        package main

import (
"fmt"

"github.com/PacktPublishing/
Go-Programming-Cookbook-Second-Edition/
chapter4/log"
)

func main() {
fmt.Println("basic logging and modification of logger:")
log.Log()
fmt.Println("logging 'handled' errors:")
log.FinalDestination()
}
  1. Run go run main.go.
  2. You may also run the following commands:
$ go build
$ ./example

You should see the following output:

$ go run main.go
basic logging and modification of logger:
logger: 2017/02/05 log.go:19: test
new logger: 2017/02/05 log.go:23: you can also add args(true)
and use Fataln to log and crash


logging 'handled' errors:
2017/02/05 18:36:11 an error occurred: in passthrougherror:
error occurred
    1. The go.mod file gets updated and the go.sum file should now be present in the top-level recipe directory.
    2. If you copied or wrote your own tests, then go up one directory and run go test. Ensure that all the tests pass.