Skip to content

utahta/go-cronowriter

Repository files navigation

cronowriter

GoDoc Reference CircleCI codecov Go Report Card GitHub release

This is a simple file writer that it writes message to the specified format path.

The file path is constructed based on current date and time, like cronolog.

This project follows the Semantic Versioning.

Installation

$ go get -u github.com/utahta/go-cronowriter

Documentation

API documentation can be found here.

The format specifications can be found here.

Examples

import "github.com/utahta/go-cronowriter"
w := cronowriter.MustNew("/path/to/example.log.%Y%m%d")
w.Write([]byte("test"))

// output file
// /path/to/example.log.20170204

You can specify the directory as below

w := cronowriter.MustNew("/path/to/%Y/%m/%d/example.log")
w.Write([]byte("test"))

// output file
// /path/to/2017/02/04/example.log

with Location

w := cronowriter.MustNew("/path/to/example.log.%Z", writer.WithLocation(time.UTC))
w.Write([]byte("test"))

// output file
// /path/to/example.log.UTC

with Symlink

w := cronowriter.MustNew("/path/to/example.log.%Y%m%d", writer.WithSymlink("/path/to/example.log"))
w.Write([]byte("test"))

// output file
// /path/to/example.log.20170204
// /path/to/example.log -> /path/to/example.log.20170204

with Mutex

w := cronowriter.MustNew("/path/to/example.log.%Y%m%d", writer.WithMutex())

no use Mutex

w := cronowriter.MustNew("/path/to/example.log.%Y%m%d", writer.WithNopMutex())

with Debug (stdout and stderr)

w := cronowriter.MustNew("/path/to/example.log.%Y%m%d", writer.WithDebug())
w.Write([]byte("test"))

// output file, stdout and stderr
// /path/to/example.log.20170204

with Init

w := cronowriter.MustNew("/path/to/example.log.%Y%m%d", writer.WithInit())

// open the file when New() method is called
// /path/to/example.log.20170204

Example using with zap

package main

import (
	"github.com/utahta/go-cronowriter"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	w1 := cronowriter.MustNew("/tmp/example.log.%Y%m%d")
	w2 := cronowriter.MustNew("/tmp/internal_error.log.%Y%m%d")
	l := zap.New(
		zapcore.NewCore(
			zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
			zapcore.AddSync(w1),
			zapcore.InfoLevel,
		),
		zap.ErrorOutput(zapcore.AddSync(w2)),
	)
	l.Info("test")
}

// output
// /tmp/example.log.20170204
// {"level":"info","ts":1486198722.1201255,"msg":"test"}

Contributing

  1. Fork it ( https://github.com/utahta/go-cronowriter/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request