En cours de développement

master
losyme 9 months ago
parent b2529c6482
commit 1c89cbe3a3
  1. 17
      format.go
  2. 7
      go.mod
  3. 2
      log.go
  4. 8
      logger.go
  5. 12
      std.go
  6. 87
      syslog.go

@ -6,17 +6,28 @@
package logger
import "forge.chapril.org/losyme/buffer"
import (
"strings"
"forge.chapril.org/losyme/buffer"
)
const (
MaxIDLen = 8
MaxNameLen = 10
_maxIDLen = 8
_maxNameLen = 10
)
const _bufSize = 256
var BufPool = buffer.NewPool(_bufSize)
func loggerLabel(id, name string) string {
a := name[:_maxNameLen]
b := id[:_maxIDLen]
return strings.Repeat(".", _maxNameLen-len(a)) + a + ":" + strings.Repeat(".", _maxIDLen-len(b)) + b
}
/*
######################################################################################################## @(°_°)@ #######
*/

@ -3,11 +3,8 @@ module forge.chapril.org/losyme/logger
go 1.17
require (
forge.chapril.org/losyme/buffer v0.0.0-20211224163712-1f575664e54c
forge.chapril.org/losyme/errors v0.0.0-20211224163814-4e7ee9f47e47
forge.chapril.org/losyme/kvfmt v0.0.0-20211224163757-e3911804b23d
forge.chapril.org/losyme/zombie v0.0.0-20211224220143-000e915df29e
)
require (
forge.chapril.org/losyme/buffer v0.0.0-20211224163712-1f575664e54c // indirect
forge.chapril.org/losyme/kvfmt v0.0.0-20211224163757-e3911804b23d // indirect
)

@ -14,8 +14,8 @@ import (
var _logPool = newLogPool()
type Log struct {
Logger *Logger
Time time.Time
Logger *Logger
Level Level
Msg string
KV []interface{}

@ -16,6 +16,7 @@ import (
type Logger struct {
id string
name string
label string
level Level
output Output
}
@ -32,6 +33,7 @@ func New(id, name, level string, output Output) (*Logger, error) {
l := &Logger{
id: id,
name: name,
label: loggerLabel(id, name),
level: StringToLevel(level),
output: output,
}
@ -51,6 +53,10 @@ func (l *Logger) Name() string {
return l.name
}
func (l *Logger) Label() string {
return l.label
}
func (l *Logger) loadLevel() Level {
return Level(atomic.LoadUint32((*uint32)(&l.level)))
}
@ -82,8 +88,8 @@ func (l *Logger) Log(level Level, msg string, kv ...interface{}) {
log := _logPool.Get()
log.Logger = l
log.Time = time.Now()
log.Logger = l
log.Level = level
log.Msg = msg
log.KV = append(log.KV, kv...)

@ -15,7 +15,7 @@ import (
)
type outputStd struct {
iow io.Writer
io.Writer
}
func (o *outputStd) Log(l *Log) error {
@ -25,9 +25,7 @@ func (o *outputStd) Log(l *Log) error {
buf.AppendByte(' ')
buf.AppendString(strings.ToUpper(l.Level.String()[:3]))
buf.AppendByte(' ')
buf.AppendRJString(l.Logger.name, '.', MaxNameLen)
buf.AppendByte('.')
buf.AppendString(l.Logger.id[:MaxIDLen])
buf.AppendString(l.Logger.label)
buf.AppendByte(' ')
buf.AppendString(l.Msg)
@ -38,7 +36,7 @@ func (o *outputStd) Log(l *Log) error {
buf.AppendString("\n")
_, err := o.iow.Write(buf.Bytes())
_, err := o.Write(buf.Bytes())
BufPool.Put(buf)
@ -51,13 +49,13 @@ func (o *outputStd) Close() error {
func NewStderrOutput() Output {
return &outputStd{
iow: os.Stderr,
Writer: os.Stderr,
}
}
func NewStdoutOutput() Output {
return &outputStd{
iow: os.Stdout,
Writer: os.Stdout,
}
}

@ -0,0 +1,87 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### logger ####### Copyright (c) 2021 losyme ################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package logger
import (
"log/syslog"
"forge.chapril.org/losyme/kvfmt"
)
type outputSyslog struct {
*syslog.Writer
}
func NewSyslogOutput(app, facility string) (Output, error) {
var p syslog.Priority
switch facility {
case "local1":
p = syslog.LOG_LOCAL1
case "local2":
p = syslog.LOG_LOCAL2
case "local3":
p = syslog.LOG_LOCAL3
case "local4":
p = syslog.LOG_LOCAL4
case "local5":
p = syslog.LOG_LOCAL5
case "local6":
p = syslog.LOG_LOCAL6
case "local7":
p = syslog.LOG_LOCAL7
default:
p = syslog.LOG_LOCAL0
}
sw, err := syslog.New(p, app)
if err != nil {
return nil, err
}
return &outputSyslog{sw}, nil
}
func (o *outputSyslog) Log(l *Log) error {
buf := BufPool.Get()
buf.AppendString(l.Logger.label)
buf.AppendByte(' ')
buf.AppendString(l.Msg)
if len(l.KV) > 0 {
buf.AppendString("> ")
kvfmt.Format(buf, l.KV...)
}
var err error
switch l.Level {
case LevelTrace:
err = o.Debug(buf.String())
case LevelDebug:
err = o.Debug(buf.String())
case LevelInfo:
err = o.Info(buf.String())
case LevelNotice:
err = o.Notice(buf.String())
case LevelWarning:
err = o.Warning(buf.String())
case LevelError:
err = o.Err(buf.String())
case LevelFatal:
err = o.Crit(buf.String())
}
BufPool.Put(buf)
return err
}
/*
######################################################################################################## @(°_°)@ #######
*/
Loading…
Cancel
Save