cronowriter

package module
Version: v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2020 License: MIT Imports: 7 Imported by: 8

README

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

uber-go/zap
package main

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

func main() {
	w1 := cronowriter.MustNew("/tmp/example.log.%Y%m%d")
	w2 := cronowriter.MustNew("/tmp/internal_error.log.%Y%m%d")
	l := zap.New(
		zap.NewJSONEncoder(),
		zap.Output(zap.AddSync(w1)),
		zap.ErrorOutput(zap.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

Documentation

Overview

Package cronowriter provides 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.

Installation

go get github.com/utahta/go-cronowriter

Format

The format specifications can be found here: https://github.com/lestrrat/go-strftime#supported-conversion-specifications

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CronoWriter

type CronoWriter struct {
	// contains filtered or unexported fields
}

A CronoWriter writes message to a set of output files.

func MustNew

func MustNew(pattern string, options ...Option) *CronoWriter

MustNew is a convenience function equivalent to New that panics on failure instead of returning an error.

func New

func New(pattern string, options ...Option) (*CronoWriter, error)

New returns a CronoWriter with the given pattern and options.

func (*CronoWriter) Close

func (c *CronoWriter) Close() error

Close closes file.

func (*CronoWriter) Path added in v1.1.0

func (c *CronoWriter) Path() string

Path returns the current writing file path.

func (*CronoWriter) Write

func (c *CronoWriter) Write(b []byte) (int, error)

Write writes to the file and rotate files automatically based on current date and time.

type Option

type Option func(*CronoWriter)

A Option with CronoWriter.

func WithDebug

func WithDebug() Option

WithDebug enables output stdout and stderr.

func WithInit

func WithInit() Option

WithInit enables its creates output file when CronoWriter initialize.

func WithLocation

func WithLocation(loc *time.Location) Option

WithLocation set the location to loc.

func WithMutex

func WithMutex() Option

WithMutex enables its uses sync.Mutex when file writing.

func WithNopMutex

func WithNopMutex() Option

WithNopMutex disables its uses sync.Mutex when file writing.

func WithStderr added in v1.1.0

func WithStderr() Option

WithStderr enables output always stderr.

func WithStdout added in v1.1.0

func WithStdout() Option

WithStdout enables output always stdout.

func WithSymlink(pattern string) Option

WithSymlink enables its creates a symbolic link to the specify pattern.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL