Skip to content

Commit

Permalink
docs: 完善README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ace-zhaoy committed Oct 1, 2024
1 parent a142986 commit d8458f8
Showing 1 changed file with 146 additions and 6 deletions.
152 changes: 146 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,148 @@
# glog
基于 zapcore.Core 实现的,可自动载入 context.Context 中的 Value,API 友好的日志库

## 功能特性
- 支持从 context.Context 中提取字段值记录到日志
- API 友好
- 支持 printf 风格
- 基于 zapcore.Core
A user-friendly logging library based on `zapcore.Core` that automatically loads values from `context.Context`.

## Features

- Supports extracting field values from `context.Context` and recording them in logs
- User-friendly API
- Supports printf-style formatting
- Based on `zapcore.Core`

## Installation

To install the package, use the following command:

```sh
go get github.com/ace-zhaoy/glog
```

## Usage

### Quick Usage
#### Default Logger
```go
package main

import (
"github.com/ace-zhaoy/glog/log"
)

func main() {
log.Info("This is an info message with context")
}

// Output:
// {"level":"info","ts":"2024-10-01T21:44:08.2737888+08:00","caller":"glog/main.go:9","msg":"This is an info message with context"}
```

#### Set Default Logger
```go
package main

import (
"context"
"github.com/ace-zhaoy/glog"
"github.com/ace-zhaoy/glog/log"
)

func main() {
logger := log.Logger()
logger = logger.WithOptions(
glog.WithContextHandlers(
glog.BuildContextHandler("request-id", "req_id"),
),
)
log.SetLogger(logger)

ctx := context.WithValue(context.Background(), "request-id", "123456")
log.InfoContext(ctx, "This is an info message with context")
}

// Output:
// {"level":"info","ts":"2024-10-01T21:41:36.825259+08:00","caller":"glog/main.go:19","msg":"This is an info message with context","req_id":"123456"}
```


### Basic Usage

```go
package main

import (
"github.com/ace-zhaoy/glog"
"go.uber.org/zap/zapcore"
)

func main() {
logger, err := glog.NewDefault()
if err != nil {
panic(err)
}

logger.Info("This is an info message")
logger.Warn("This is a warning message")
logger.Error("This is an error message")
}
```

### Contextual Logging

```go
package main

import (
"context"
"github.com/ace-zhaoy/glog"
)

func main() {
logger, err := glog.NewDefault(
glog.WithContextHandlers(
glog.BuildContextHandler("request_id"),
),
)
if err != nil {
panic(err)
}

ctx := context.WithValue(context.Background(), "request_id", "12345")
logger.WithContext(ctx).Info("This is an info message with context")
logger.InfoContext(ctx, "This is an info message")
}
```

### Customizing Logger

```go
package main

import (
"github.com/ace-zhaoy/glog"
"go.uber.org/zap/zapcore"
"os"
)

func main() {
core := zapcore.NewCore(
zapcore.NewJSONEncoder(zapcore.EncoderConfig{}),
zapcore.AddSync(os.Stdout),
zapcore.DebugLevel,
)

logger := glog.NewLogger(core)
logger.Info("This is a custom logger message")
}
```

## Testing

To run the tests, use the following command:

```sh
go test ./...
```

## License

This project is licensed under the MIT License.

0 comments on commit d8458f8

Please sign in to comment.