Skip to content

Commit

Permalink
Update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
utahta committed Jul 29, 2017
1 parent f6f4a08 commit ab0c298
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 41 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: go
go:
- 1.6
- 1.7
- 1.8
- tip
sudo: false
install:
Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Yuta Uekusa

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
# CronoWriter
# cronowriter

[![GitHub release](https://img.shields.io/github/release/utahta/go-cronowriter.svg)](https://github.com/utahta/go-cronowriter/releases)
[![GoDoc Reference](https://godoc.org/github.com/utahta/go-cronowriter?status.svg)](http://godoc.org/github.com/utahta/go-cronowriter)
[![Build Status](https://travis-ci.org/utahta/go-cronowriter.svg?branch=master)](https://travis-ci.org/utahta/go-cronowriter)
[![Go Report Card](https://goreportcard.com/badge/github.com/utahta/go-cronowriter)](https://goreportcard.com/report/github.com/utahta/go-cronowriter)
[![GitHub release](https://img.shields.io/github/release/utahta/go-cronowriter.svg)](https://github.com/utahta/go-cronowriter/releases)

This is a simple file writer that writes message to a set of output files, the names of which are constructed time-based format like cronolog.
This is a simple file writer that it writes message to the output files.

## Install
The output file names are constructed time-based format like cronolog.

## Installation

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

## Usage
## Documentation

API documentation can be found [here](http://godoc.org/github.com/utahta/go-cronowriter).

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

## Examples

```go
import "github.com/utahta/go-cronowriter"
Expand All @@ -25,7 +35,7 @@ w.Write([]byte("test"))
// /path/to/example.log.20170204
```

if you specify a directory
You can specify the directory as below
```go
w := cronowriter.MustNew("/path/to/%Y/%m/%d/example.log")
w.Write([]byte("test"))
Expand Down Expand Up @@ -75,11 +85,7 @@ w := cronowriter.MustNew("/path/to/example.log.%Y%m%d", writer.WithInit())
// /path/to/example.log.20170204
```

## Format

See [lestrrat/go-strftime#supported-conversion-specifications](https://github.com/lestrrat/go-strftime#supported-conversion-specifications)

## Combination
## Example using with zap

### [uber-go/zap](https://github.com/uber-go/zap)

Expand Down
14 changes: 14 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Package cronowriter provides a simple file writer that it writes message to the output files.
The output file names are constructed time-based format 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
*/
package cronowriter
26 changes: 14 additions & 12 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ import (
"os"
)

type logger interface {
Write(b []byte)
Println(args ...interface{})
Printf(format string, args ...interface{})
Error(args ...interface{})
Errorf(format string, args ...interface{})
}
type (
logger interface {
Write(b []byte)
Println(args ...interface{})
Printf(format string, args ...interface{})
Error(args ...interface{})
Errorf(format string, args ...interface{})
}

type nopLogger struct{}
nopLogger struct{}

type debugLogger struct {
stdout io.Writer
stderr io.Writer
}
debugLogger struct {
stdout io.Writer
stderr io.Writer
}
)

func newDebugLogger() *debugLogger {
return &debugLogger{
Expand Down
52 changes: 34 additions & 18 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@ import (
"github.com/lestrrat/go-strftime"
)

type CronoWriter struct {
pattern *strftime.Strftime // given pattern
path string // current file path
symlink *strftime.Strftime // symbolic link to current file path
fp *os.File // current file pointer
loc *time.Location
mux sync.Locker
debug logger
init bool // if true, open the file when New() method is called
}

type Option func(*CronoWriter)
type (
// A CronoWriter writes message to a set of output files.
CronoWriter struct {
pattern *strftime.Strftime // given pattern
path string // current file path
symlink *strftime.Strftime // symbolic link to current file path
fp *os.File // current file pointer
loc *time.Location
mux sync.Locker
debug logger
init bool // if true, open the file when New() method is called
}

// A CronoWriter Option.
Option func(*CronoWriter)
)

var (
_ io.WriteCloser = &CronoWriter{} // check if object implements interface
now func() time.Time = time.Now // for test
_ io.WriteCloser = (*CronoWriter)(nil) // check if object implements interface
now func() time.Time = time.Now // for test
)

// New returns a CronoWriter with the given pattern and options.
Expand All @@ -41,7 +45,7 @@ func New(pattern string, options ...Option) (*CronoWriter, error) {
symlink: nil,
fp: nil,
loc: time.Local,
mux: new(nopMutex), // default mutex off
mux: new(sync.Mutex), // default mutex enable
debug: &nopLogger{},
init: false,
}
Expand All @@ -68,12 +72,14 @@ func MustNew(pattern string, options ...Option) *CronoWriter {
return c
}

// WithLocation set the location to loc.
func WithLocation(loc *time.Location) Option {
return func(c *CronoWriter) {
c.loc = loc
}
}

// WithSymlink enables its creates a symbolic link to the specify pattern.
func WithSymlink(pattern string) Option {
return func(c *CronoWriter) {
p, err := strftime.New(pattern)
Expand All @@ -84,18 +90,28 @@ func WithSymlink(pattern string) Option {
}
}

// WithMutex enables its uses sync.Mutex when file writing.
func WithMutex() Option {
return func(c *CronoWriter) {
c.mux = new(sync.Mutex)
}
}

// WithNopMutex disables its uses sync.Mutex when file writing.
func WithNopMutex() Option {
return func(c *CronoWriter) {
c.mux = new(nopMutex)
}
}

// WithDebug enables output stdout and stderr.
func WithDebug() Option {
return func(c *CronoWriter) {
c.debug = newDebugLogger()
}
}

// WithInit enables its creates output file when CronoWriter initialize.
func WithInit() Option {
return func(c *CronoWriter) {
c.init = true
Expand Down Expand Up @@ -144,17 +160,17 @@ func (c *CronoWriter) createSymlink(t time.Time, path string) {
symlink := c.symlink.FormatString(t)
if symlink == path {
c.debug.Error("Can't create symlink. same path is specified.")
return
return // ignore error
}

if err := os.Remove(symlink); err != nil {
c.debug.Error(err)
return
return // ignore error
}

if err := os.Symlink(path, symlink); err != nil {
c.debug.Error(err)
// ignore error
return // ignore error
}
}

Expand Down

0 comments on commit ab0c298

Please sign in to comment.