j

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2019 License: MIT Imports: 8 Imported by: 0

README

github.com/zhengkai/j

GoDoc Travis-CI Build Status Coverage Status MIT License

Package j implements a simple logging package. Easy to use.

It has more feature than standard logger pkg, for example:

  • log files rotation
  • background (tunnel) write
  • ANSI color pre line
  • temporarily disable

Documentation

Overview

Package j implements a simple logging package. Easy to use.

It has more feature than standard logger pkg, for example:

· log files rotation

· background (tunnel) write

· ANSI color pre line

· temporarily disable

Index

Examples

Constants

View Source
const (
	Echo = configKey(iota + 1)
	Append
	Prefix
	TimeFormat
	Tunnel
	LineFn
	Caller
	PermFile
	PermDir
	ErrorFn
)

Config key, used by SetDefault()

View Source
const (
	CallerNone    = callerType(iota + 1)
	CallerShort   // caller.go:42
	CallerShorter // caller:42
	CallerLong    // /dir/caller.go:42
)

just like Lshortfile/Llongfile in pkg/log

View Source
const (
	TimeMS      = `15:04:05.000 `
	TimeDayMS   = `02 15:04:05.000 `
	TimeMonthMS = `01-02 15:04:05.000 `
	TimeYearMS  = `2006-01-02 15:04:05.000 `
	Time        = `15:04:05 `
	TimeDay     = `02 15:04:05 `
	TimeMonth   = `01-02 15:04:05 `
	TimeYear    = `2006-01-02 15:04:05 `
	TimeNS      = `15:04:05.000000000 `
)

time format in config

Variables

View Source
var (
	ErrTunnelOverflow = errors.New(`Tunnel overflow, log dropped`)
)

Errors

Functions

func GetDefault added in v1.0.2

func GetDefault() (m map[string]interface{})

GetDefault ...

func SetDefault added in v1.0.2

func SetDefault(k configKey, v interface{}) (ok bool)

SetDefault for new logger

func UnsetDefault added in v1.0.2

func UnsetDefault(k configKey)

UnsetDefault ...

Types

type Config

type Config struct {

	// fill only one of them
	File     *os.File
	FileFn   FileFunc
	Filename string

	Echo       bool // stdout
	Append     bool
	Prefix     string
	TimeFormat string
	Tunnel     int // channel buffer size, see also Close()
	LineFn     LineFunc
	ErrorFn    ErrorFunc
	Caller     callerType
	PermFile   os.FileMode
	PermDir    os.FileMode
}

Config for create logger

type ErrorFunc added in v1.0.3

type ErrorFunc func(o *Logger)

ErrorFunc is callback when trigger error

type FileFunc added in v1.0.2

type FileFunc func(t *time.Time) (filename string)

FileFunc is the type of the function called for getting filename. If the filename is different from the previous one, a new log file will be created

type LineFunc added in v1.0.2

type LineFunc func(line *string)

LineFunc is the type of the function called by from Log / Logf / Print / Compact, but Raw.

Example
package main

import (
	"regexp"

	"github.com/zhengkai/j"
)

func main() {
	re := regexp.MustCompile(`(foo|bar)`)
	repl := " [ $1 ] "
	fn := func(s *string) {
		r := re.ReplaceAllString(*s, repl)
		*s = r
	}

	x := j.New(&j.Config{
		LineFn: fn,
	})

	x.Log(`afoob`)
}
Output:
17:04:31.829 example_test.go:29 a [ foo ] b

type Logger

type Logger struct {
	Error error
	// contains filtered or unexported fields
}

Logger will be always returned by New series func, if there is any error, recorded in Error

func New

func New(c *Config) (o *Logger)

New create a new logger

func NewEcho

func NewEcho() (o *Logger)

NewEcho create a new logger without file, only stdout

func NewFile

func NewFile(filename string) (o *Logger)

NewFile create a new logger with filename

func NewFunc

func NewFunc(fileFn FileFunc) (o *Logger)

NewFunc create a new logger with FileFunc

Example
package main

import (
	"time"

	"github.com/zhengkai/j"
)

func main() {
	jx := j.NewFunc(func(t *time.Time) string {
		return t.Format(`log/01-02/15.log`)
	})
	jx.Log(`file func`)
}

func NewPure added in v1.0.2

func NewPure(c *Config) (o *Logger)

NewPure create a new logger without default config

func (*Logger) BR

func (o *Logger) BR() (err error)

BR add an empty line

func (*Logger) Close

func (o *Logger) Close()

Close permanently disable Log() series. log files will be closed, unless come from SetFile() or config File.

Note: if create by config Tunnel, must use Close() to wait that all logs have been processed

func (*Logger) Color

func (o *Logger) Color(s string) (err error)

Color use ANSI color for each line

func (*Logger) ColorOnce

func (o *Logger) ColorOnce(s string) (err error)

ColorOnce use ANSI color, only next line

func (*Logger) ColorStop added in v1.0.2

func (o *Logger) ColorStop() (err error)

ColorStop no longer use ANSI color

func (*Logger) Compact

func (o *Logger) Compact(a ...interface{}) (err error)

Compact just like fmt.Print, but no spaces

func (*Logger) Enable

func (o *Logger) Enable(is bool)

Enable used to temporarily disable

func (*Logger) GetFile added in v1.0.2

func (o *Logger) GetFile() *os.File

GetFile ...

func (*Logger) Log

func (o *Logger) Log(a ...interface{}) (err error)

Log just like log.Println

func (*Logger) Logf

func (o *Logger) Logf(format string, a ...interface{}) (err error)

Logf just like log.Printf

func (*Logger) Print

func (o *Logger) Print(a ...interface{}) (err error)

Print just like log.Print, added linebreak

func (*Logger) Raw

func (o *Logger) Raw(a ...interface{}) (err error)

Raw log raw (no time, no linebreak, no spaces), will not trigger lineFn(LineFunc)

func (*Logger) SetFile added in v1.0.2

func (o *Logger) SetFile(f *os.File)

SetFile ...

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL