Skip to content

Commit

Permalink
add some methods for Record
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 15, 2020
1 parent 1693891 commit b097a46
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 58 deletions.
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
*.log
*.swp
.idea
*.patch
*.tmp
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
/alone
/cliapp
.DS_Store
# shell script
/*.bash
/*.sh
/*.zsh
/*.pid
go.sum
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2016 inhere

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.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

simple log

> Inspired the projects [Seldaek/monolog](https://github.com/Seldaek/monolog) and [sirupsen/logrus](https://github.com/sirupsen/logrus). Thank you very much
## GoDoc

- [godoc for github](https://pkg.go.dev/github.com/gookit/slog?tab=doc)
Expand All @@ -12,7 +14,19 @@ simple log
go get github.com/gookit/slog
```

##
## Usage

```go
package main

import (
"github.com/gookit/slog"
)

func main() {
slog.Infof("info log %s", "message")
}
```

## Workflow

Expand All @@ -29,9 +43,13 @@ Logger -{
- https://github.com/golang/glog
- https://github.com/Seldaek/monolog

## Related Pkg
## Related

- https://github.com/sirupsen/logrus
- https://github.com/uber-go/zap
- https://github.com/rs/zerolog
- https://github.com/syyongx/llog

## LICENSE

[MIT](LICENSE)
49 changes: 25 additions & 24 deletions define.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package slog
import (
"fmt"
"strings"
"time"
)

// M short name of map[string]interface{}
Expand All @@ -19,52 +20,47 @@ type StringMap map[string]string
type Level uint32

// String get level name
func (l Level) String() string {
func (l Level) String() string {
return LevelName(l)
}

// Name get level name
func (l Level) Name() string {
func (l Level) Name() string {
return LevelName(l)
}

// These are the different logging levels. You can set the logging level to log
// on your instance of logger, obtained with `logrus.New()`.
// These are the different logging levels. You can set the logging level to log handler
const (
// PanicLevel level, highest level of severity. Logs and then calls panic with the
// message passed to Debug, Info, ...
PanicLevel Level = iota + 1
// PanicLevel level, highest level of severity.
PanicLevel Level = 100
// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
// logging level is set to Panic.
FatalLevel
FatalLevel Level = 200
// ErrorLevel level. Runtime errors. Used for errors that should definitely be noted.
// Commonly used for hooks to send errors to an error tracking service.
ErrorLevel
ErrorLevel Level = 300
// WarnLevel level. Non-critical entries that deserve eyes.
WarnLevel
WarnLevel Level = 400
// NoticeLevel level Uncommon events
NoticeLevel
NoticeLevel Level = 500
// InfoLevel level. Examples: User logs in, SQL logs.
InfoLevel
InfoLevel Level = 600
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
DebugLevel
DebugLevel Level = 700
// TraceLevel level. Designates finer-grained informational events than the Debug.
TraceLevel
TraceLevel Level = 800
)

var (
DefaultChannelName = "application"
DefaultTimeFormat = "2006/01/02 15:04:05"
)
const flushInterval = 30 * time.Second

const (
FieldKeyTime = "time"
FieldKeyData = "data"
FieldKeyFunc = "func"
FieldKeyFile = "file"
FieldKeyTime = "time"
FieldKeyData = "data"
FieldKeyFunc = "func"
FieldKeyFile = "file"
// FieldKeyDate = "date"

FieldKeyDatetime = "datetime"
FieldKeyDatetime = "datetime"

FieldKeyLevel = "level"
FieldKeyError = "error"
Expand All @@ -74,6 +70,11 @@ const (
FieldKeyMessage = "message"
)

var (
DefaultChannelName = "application"
DefaultTimeFormat = "2006/01/02 15:04:05"
)

// AllLevels exposing all logging levels
var AllLevels = []Level{
PanicLevel,
Expand Down Expand Up @@ -140,4 +141,4 @@ func Name2Level(ln string) (Level, error) {

var l Level
return l, fmt.Errorf("invalid log Level: %q", ln)
}
}
5 changes: 4 additions & 1 deletion formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ func TestJSONFormatter(t *testing.T) {

l.AddHandler(h)

l.Info("info message")
l.WithFields(slog.M{
"field1": 123,
"field2": "abc",
}).Info("info message")

// PrettyPrint=true

Expand Down
63 changes: 49 additions & 14 deletions logger.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package slog

import (
"context"
"fmt"
"os"
"sync"
"time"
)

const flushInterval = 30 * time.Second

// Logger definition
type Logger struct {
name string
Expand Down Expand Up @@ -63,7 +62,9 @@ func (logger *Logger) releaseRecord(r *Record) {
}

//
// ------------ Management ------------
// ---------------------------------------------------------------------------
// Management
// ---------------------------------------------------------------------------
//

// Sync flushes buffered logs (if any).
Expand Down Expand Up @@ -94,7 +95,7 @@ func (logger *Logger) lockAndFlushAll() {
func (logger *Logger) FlushAll() {
// Flush from fatal down, in case there's trouble flushing.
for _, handler := range logger.handlers {
handler.Flush()
_= handler.Flush() // ignore error
}
}

Expand Down Expand Up @@ -122,7 +123,9 @@ func (logger *Logger) Name() string {
}

//
// ------------ Register handlers and processors ------------
// ---------------------------------------------------------------------------
// Register handlers and processors
// ---------------------------------------------------------------------------
//

// AddHandler to the logger
Expand Down Expand Up @@ -166,7 +169,47 @@ func (logger *Logger) SetProcessors(ps []Processor) {
}

//
// ---------- Add logs with level ----------
// ---------------------------------------------------------------------------
// New record with log data, fields
// ---------------------------------------------------------------------------
//

// SetFields new record with fields
func (logger *Logger) WithFields(fields M) *Record {
r := logger.newRecord()
defer logger.releaseRecord(r)

return r.SetFields(fields)
}

// WithData new record with data
func (logger *Logger) WithData(data M) *Record {
r := logger.newRecord()
defer logger.releaseRecord(r)

return r.SetData(data)
}

// WithTime new record with time.Time
func (logger *Logger) WithTime(t time.Time) *Record {
r := logger.newRecord()
defer logger.releaseRecord(r)

return r.SetTime(t)
}

// WithContext new record with context.Context
func (logger *Logger) WithContext(ctx context.Context) *Record {
r := logger.newRecord()
defer logger.releaseRecord(r)

return r.SetContext(ctx)
}

//
// ---------------------------------------------------------------------------
// Add log message with level
// ---------------------------------------------------------------------------
//

// Log an message
Expand All @@ -185,14 +228,6 @@ func (logger *Logger) Logf(level Level, format string, args ...interface{}) {
logger.releaseRecord(r)
}

// WithField with new fields to record
func (logger *Logger) WithFields(fields M) *Record {
r := logger.newRecord()
defer logger.releaseRecord(r)

return r.WithFields(fields)
}

// Warning logs a message at level Warn
func (logger *Logger) Warning(args ...interface{}) {
logger.Warn(args...)
Expand Down
Loading

0 comments on commit b097a46

Please sign in to comment.