Skip to content

Commit

Permalink
docs: add logger info & add sitemap & rm blog
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayheaven committed Jan 19, 2024
1 parent 6b2005c commit 501c33a
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 14,767 deletions.
12 changes: 0 additions & 12 deletions docs/go-gin-boilerplate/blog/2019-05-28-first-blog-post.md

This file was deleted.

44 changes: 0 additions & 44 deletions docs/go-gin-boilerplate/blog/2019-05-29-long-blog-post.md

This file was deleted.

20 changes: 0 additions & 20 deletions docs/go-gin-boilerplate/blog/2021-08-01-mdx-blog-post.mdx

This file was deleted.

Binary file not shown.
25 changes: 0 additions & 25 deletions docs/go-gin-boilerplate/blog/2021-08-26-welcome/index.md

This file was deleted.

17 changes: 0 additions & 17 deletions docs/go-gin-boilerplate/blog/authors.yml

This file was deleted.

5 changes: 3 additions & 2 deletions docs/go-gin-boilerplate/docs/guide/_category_.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"label": "指南",
"label": "使用指南",
"position": 3,
"link": {}
"link": {},
"collapsed": false
}
5 changes: 0 additions & 5 deletions docs/go-gin-boilerplate/docs/guide/error.md

This file was deleted.

155 changes: 155 additions & 0 deletions docs/go-gin-boilerplate/docs/guide/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,158 @@ sidebar_position: 5
---

# 日志

日志是一个记录事件的过程,例如:记录应用程序的运行状态、记录用户的操作行为等。

## 介绍

Go Gin Boilerplate 集成了两种主流的日志库,分别是 [logrus](https://github.com/sirupsen/logrus)[zap](https://github.com/uber-go/zap)

## 需求

在开发过程中,我们需要记录应用程序的运行状态,例如:记录用户的操作行为、记录应用程序的运行状态等。

我们通常会有以下几点主要的需求:

- 日志输出到文件、控制台
- 日志分级,例如:`debug``info``warn``error``fatal``panic`
- 日志切割,按照时间或者大小切割

## 日志包

我们定义了一个日志包,在 `internal/pkg/logger` 目录下,用于封装日志库的使用。

其中 `logger.go` 作为日志包的入口,定义了日志的初始化方法 `init()`,用于初始化日志库。

默认选择 **logrus** 作为日志库,如果需要使用 zap 作为日志库,可以修改 `logger.go` 中的初始化方法。

```go
var (
ZapLogger *zap.Logger
ZapSugar *zap.SugaredLogger

LogrusLogger *logrus.Logger
)

func init() {
LogrusLogger = InitLogrusLogger()

// ZapLogger = InitZapLogger()
// ZapSugar = ZapLogger.Sugar()
}
```

## logrus

logrus 是一个结构化的日志库,提供了丰富的日志输出格式,例如:JSON、Text、Logfmt 等。

`logger/logrus.go` 中,我们定义了一个初始化方法 `InitLogrusLogger()`,用于初始化 logrus 日志库,返回一个 `logrus.Logger` 实例。

```go
func InitLogrusLogger() *logrus.Logger {
logger := logrus.New()

logger.SetLevel(logrus.TraceLevel)
logger.SetFormatter(&logrus.JSONFormatter{})

logFile := &lumberjack.Logger{
Filename: "logs/app.log",
MaxSize: 10,
Compress: true,
}

// multi writer, both file and stdout
writers := []io.Writer{logFile, os.Stdout}
logger.SetOutput(io.MultiWriter(writers...))

return logger
}
```

## zap

zap 是一个高性能的日志库,提供了丰富的日志输出格式,例如:JSON、Text、Logfmt 等。

`logger/zap.go` 中,我们定义了一个初始化方法 `InitZapLogger()`,用于初始化 zap 日志库,返回一个 `zap.Logger` 实例。

```go
func InitZapLogger() *zap.Logger {

encoder := getEncoder()

// First, define our level-handling logic.

// level: debug,info,warning
infoLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl < zapcore.ErrorLevel
})
// level: error, dpanic, panic, fatal
errorLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
})

infoLevel_writerSyncer := getWriterSyncer("info")
errorLevel_writerSyncer := getWriterSyncer("error")

info_multiWriteSyncer := zapcore.NewMultiWriteSyncer(infoLevel_writerSyncer, os.Stdout)
error_multiWriteSyncer := zapcore.NewMultiWriteSyncer(errorLevel_writerSyncer, os.Stdout)

core := zapcore.NewCore(encoder, info_multiWriteSyncer, infoLevel)
errorCore := zapcore.NewCore(encoder, error_multiWriteSyncer, errorLevel)

coreArr := []zapcore.Core{core, errorCore}

// export
zapLogger := zap.New(zapcore.NewTee(coreArr...), zap.AddCaller()) // zap.AddCaller() will add line number and file name
return zapLogger

}
```

其中 两个内部函数 `getEncoder()``getWriterSyncer()` 分别用于获取日志编码器和日志输出器。

```go

func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
return zapcore.NewJSONEncoder(encoderConfig)
}

func getWriterSyncer(level string) zapcore.WriteSyncer {
lumberWriteSyncer := &lumberjack.Logger{
Filename: fmt.Sprintf("logs/%s.log", level),
MaxSize: 10, // megabytes
Compress: true,
}
// file, _ := os.Create("logs/app.log")
return zapcore.Lock(zapcore.AddSync(lumberWriteSyncer))

}
```

## lumberjack

lumberjack 是一个日志切割库,可以按照时间或者大小切割日志文件。

在 LogrusLogger 和 ZapLogger 中,我们都使用了 lumberjack 日志切割库。

```go
// logrus.go
logFile := &lumberjack.Logger{
Filename: "logs/app.log",
MaxSize: 10,
Compress: true,
}
```

```go
// zap.go
lumberWriteSyncer := &lumberjack.Logger{
Filename: fmt.Sprintf("logs/%s.log", level),
MaxSize: 10, // megabytes
Compress: true,
}
```
8 changes: 7 additions & 1 deletion docs/go-gin-boilerplate/docs/guide/middleware.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 4
sidebar_position: 2.3
---

# 中间件
Expand Down Expand Up @@ -220,3 +220,9 @@ func Logger() gin.HandlerFunc {
}
}
```

## 更多

更多 Gin 中间件参考

- [gin-contrib](https://github.com/orgs/gin-contrib/repositories?q=middleware&type=all&language=&sort=)
2 changes: 1 addition & 1 deletion docs/go-gin-boilerplate/docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sidebar_position: 1

<div>

<img src="/img/golang.png" align="right"/>
<img src="/go-gin-boilerplate/img/golang.png" align="right"/>

![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/sanjayheaven/go-gin-boilerplate)
![CI](https://github.com/sanjayheaven/go-gin-boilerplate/workflows/Go/badge.svg)
Expand Down
11 changes: 8 additions & 3 deletions docs/go-gin-boilerplate/docusaurus.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { themes as prismThemes } from "prism-react-renderer";
import type { Config } from "@docusaurus/types";
import type * as Preset from "@docusaurus/preset-classic";
import { EnumChangefreq } from "sitemap";

const config: Config = {
title: "Go Gin Boilerplate",
Expand Down Expand Up @@ -32,7 +33,7 @@ const config: Config = {

presets: [
[
"classic",
"@docusaurus/preset-classic",
{
docs: {
sidebarPath: "./sidebars.ts",
Expand All @@ -49,9 +50,13 @@ const config: Config = {
editUrl:
"https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/",
},
theme: {
customCss: "./src/css/custom.css",
theme: { customCss: "./src/css/custom.css" },
sitemap: {
changefreq: EnumChangefreq.WEEKLY,
priority: 0.5,
filename: "sitemap.xml",
},
// gtag: {},
} satisfies Preset.Options,
],
],
Expand Down
Loading

0 comments on commit 501c33a

Please sign in to comment.