master v0.1.8
guohao 5 years ago
parent c72713582b
commit eff7a852ec
  1. 10
      .gitlab-ci.yml
  2. 6
      filelog.go
  3. 114
      gokit.go
  4. 6
      gokit_test.go
  5. 2
      syslog.go

@ -1,10 +0,0 @@
check:
tags:
- k8s
stage: build
only:
- branches
except:
- beta
script:
- go test

@ -79,7 +79,7 @@ func NewFileLogger(path, name string, flushInterval time.Duration, fileSplitSize
flushInterval: flushInterval, flushInterval: flushInterval,
fileSplitSize: fileSplitSize, fileSplitSize: fileSplitSize,
bufferSize: bufferSize, bufferSize: bufferSize,
writer: &bufferWriter{ writer: &bufferWriter{
logPath: path, logPath: path,
logName: name, logName: name,
maxFileSize: fileSplitSize, maxFileSize: fileSplitSize,
@ -112,7 +112,7 @@ func (w *mFileLogger) flush() (err error) {
return return
} }
func (w *mFileLogger) Write(msg []byte) (n int, err error) { func (w *mFileLogger) Write(msg []byte) (n int, err error) {
buf := w.getBuffer() buf := w.getBuffer()
buf.Write(msg) buf.Write(msg)
@ -137,7 +137,7 @@ func (w *mFileLogger) Write(msg []byte) (n int, err error) {
} }
w.putBuffer(buf) w.putBuffer(buf)
return writer.Write(buf.Bytes()) return writer.Write(buf.Bytes())
} }
func (bufferW *bufferWriter) Write(p []byte) (int, error) { func (bufferW *bufferWriter) Write(p []byte) (int, error) {

@ -1,15 +1,19 @@
package logkit package logkit
import ( import (
"flag"
"fmt" "fmt"
"io" "io"
"path" "path"
"runtime" "runtime"
"strconv"
"strings"
"time" "time"
) )
var ( var (
inited bool inited bool
auto bool
logWriter io.Writer logWriter io.Writer
logLevel = LevelInfo logLevel = LevelInfo
logLevelName string logLevelName string
@ -42,8 +46,35 @@ const (
LevelFatal LevelFatal
) )
func (l *Level) String() string {
return levelToNames[*l]
}
// Get is part of the flag.Value interface.
func (l *Level) Get() interface{} {
return *l
}
func (l *Level) Set(value string) error {
for i, name := range levelToNames {
if strings.ToUpper(value) == name {
*l = i
}
}
if *l == Default {
v, err := strconv.Atoi(value)
if err != nil {
return err
}
*l = Level(v)
}
if *l == Default {
*l = LevelDebug
}
return nil
}
type Channel byte type Channel byte
type Caller byte
const ( const (
FIlE Channel = iota FIlE Channel = iota
@ -51,6 +82,29 @@ const (
KAFKA KAFKA
) )
func (c *Channel) String() string {
switch *c {
case FIlE:
return "file"
case SYSLOG:
return "syslog"
}
return "file"
}
func (c *Channel) Set(value string) error {
switch value {
case "file":
*c = FIlE
case "syslog":
*c = SYSLOG
default:
*c = FIlE
}
return nil
}
type Caller byte
const ( const (
_ Caller = iota _ Caller = iota
NONE NONE
@ -59,6 +113,33 @@ const (
BasePath BasePath
) )
func (c *Caller) String() string {
switch *c {
case NONE:
return "none"
case FullPATHFunc:
return "full"
case BasePathFunc:
return "file_func"
case BasePath:
return "file"
}
return "file"
}
func (c *Caller) Set(value string) error {
switch value {
case "file":
*c = BasePath
case "file_func":
*c = BasePathFunc
case "full":
*c = FullPATHFunc
default:
*c = BasePathFunc
}
return nil
}
type Writer interface { type Writer interface {
//Write 写日志 //Write 写日志
Write(msg []byte) (int, error) Write(msg []byte) (int, error)
@ -66,11 +147,32 @@ type Writer interface {
Close() error Close() error
} }
func GetWriter() io.Closer {
return logWriter.(io.Closer)
}
func Exit() { func Exit() {
logWriter.(io.Closer).Close() logWriter.(io.Closer).Close()
} }
func Init(_channel Channel, name string, level Level, _alsoStdout bool, _withCaller Caller) (writer io.Writer, err error ){ func init() {
flag.Var(&logLevel, "log.level", "log level, default `INFO`, it can be `DEBUG, INFO, WARN, ERROR, FATAL`")
flag.Var(&withCaller, "log.withcaller", "call context, by default filename and func name, it can be `file, file_func, full`")
flag.Var(&channel, "log.channel", "write to , it can be `file syslog`")
flag.BoolVar(&alsoStdout, "log.alsostdout", false, "log out to stand error as well, default `false`")
flag.StringVar(&logName, "log.name", "", "log name, by default log will out to `/data/logs/{name}.log`")
flag.BoolVar(&auto, "log.autoinit", true, "log will be init automatic")
if auto {
println("----", logLevel)
_, err := Init(channel, logName, logLevel, alsoStdout, withCaller)
if err != nil {
println("logkit init fail, ", err.Error())
}
}
}
func Init(_channel Channel, name string, level Level, _alsoStdout bool, _withCaller Caller) (writer io.Writer, err error) {
if inited { if inited {
return nil, fmt.Errorf("logkit has been inited") return nil, fmt.Errorf("logkit has been inited")
} }
@ -85,7 +187,7 @@ func Init(_channel Channel, name string, level Level, _alsoStdout bool, _withCal
if logPath == "" { if logPath == "" {
logPath = "/data/logs/" + logName + ".log" logPath = "/data/logs/" + logName + ".log"
} }
logWriter, err = NewFileLogger(logPath, logName, time.Second*5, 1204*1024*1800, 4*1024) logWriter, err = NewFileLogger(logPath, logName, time.Second*5, 1204*1024*1800, 4*1024)
if err != nil { if err != nil {
return return
} }
@ -123,11 +225,11 @@ func format(level Level, msg string) string {
case FullPATHFunc: case FullPATHFunc:
context = fmt.Sprintf("%s:%03d::%30s", file, line, path.Base(runtime.FuncForPC(pc).Name())) context = fmt.Sprintf("%s:%03d::%30s", file, line, path.Base(runtime.FuncForPC(pc).Name()))
case BasePathFunc: case BasePathFunc:
context = fmt.Sprintf("%s:%03d::%30s", path.Base(file), line, path.Base(runtime.FuncForPC(pc).Name())) context = fmt.Sprintf("%s:%03d::%30s", path.Base(file), line, path.Base(runtime.FuncForPC(pc).Name()))
case BasePath: case BasePath:
context = fmt.Sprintf("%s:%03d", path.Base(file), line) context = fmt.Sprintf("%s:%03d", path.Base(file), line)
default: default:
context = fmt.Sprintf("%s:%03d", path.Base(file), line) context = fmt.Sprintf("%s:%03d", path.Base(file), line)
} }
return fmt.Sprintf("%s\t[%4s]\t%s\t%s\n", time.Now().Format("2006-01-02 15:04:05.999"), getLevelName(level), context, msg) return fmt.Sprintf("%s\t[%4s]\t%s\t%s\n", time.Now().Format("2006-01-02 15:04:05.999"), getLevelName(level), context, msg)

@ -7,10 +7,6 @@ import (
"time" "time"
) )
func init() {
}
func BenchmarkGoKit(b *testing.B) { func BenchmarkGoKit(b *testing.B) {
defer Exit() defer Exit()
SetPath("/") SetPath("/")
@ -53,5 +49,5 @@ func TestBuffer(t *testing.T) {
//Exit() //Exit()
} }
func TestFlag(t *testing.T) { func TestFlag(t *testing.T) {
} }

@ -51,7 +51,7 @@ func (self *SyslogWriter) Write(msg []byte) (int, error) {
return self.writer.Write([]byte(msg)) return self.writer.Write([]byte(msg))
} }
func (self *SyslogWriter) Close() error{ func (self *SyslogWriter) Close() error {
// ignore the error return code // ignore the error return code
return self.writer.Close() return self.writer.Close()
} }

Loading…
Cancel
Save