-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# 文件控制台同时输出 | ||
|
||
## 如何在 go-zero 中输出日志到文件中的同时也打印到控制台? | ||
|
||
为了在使用 go-zero 框架时实现日志既输出到文件又打印到控制台,可以按照以下步骤进行配置和编写代码。 | ||
|
||
**步骤如下:** | ||
|
||
1. **创建配置文件 `config.yaml`**: | ||
首先,定义一个 YAML 文件来配置日志输出模式和编码方式。 | ||
|
||
```yaml | ||
Mode: file | ||
Encoding: json | ||
``` | ||
2. **编写主程序 `main.go`**: | ||
使用以下 Go 代码加载配置文件,并设置日志输出到文件和控制台。 | ||
|
||
```go | ||
package main | ||
import ( | ||
"os" | ||
"time" | ||
"github.com/zeromicro/go-zero/core/conf" | ||
"github.com/zeromicro/go-zero/core/logx" | ||
"github.com/zeromicro/go-zero/core/proc" | ||
) | ||
func main() { | ||
var c logx.LogConf | ||
conf.MustLoad("config.yaml", &c) // 加载配置文件 | ||
logx.MustSetup(c) // 设置日志配置 | ||
logx.AddWriter(logx.NewWriter(os.Stdout)) // 添加控制台输出 | ||
for { | ||
select { | ||
case <-proc.Done(): // 检查程序是否需要退出 | ||
return | ||
default: | ||
time.Sleep(time.Second) | ||
logx.Info(time.Now()) // 打印当前时间到日志 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
**详细说明**: | ||
|
||
- **配置文件 (`config.yaml`)**: | ||
- `Mode: file` 表示将日志输出到文件。 | ||
- `Encoding: json` 指定日志的编码格式为 JSON。 | ||
|
||
- **主程序 (`main.go`)**: | ||
- 使用 `conf.MustLoad` 加载配置文件。 | ||
- 调用 `logx.MustSetup` 配置日志系统。 | ||
- 使用 `logx.AddWriter` 方法添加额外的日志输出目标,这里我们添加了标准输出(控制台)。 | ||
- 在无限循环中,每秒记录一次当前时间,通过 `select` 语句配合 `proc.Done()` 实现平滑退出。 | ||
|
||
通过以上配置和代码,能够实现 go-zero 同时输出日志到文件和控制台。 | ||
|
||
> go-zero 版本:>= v1.7.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
title: 'FAQ' | ||
slug: /docs/faq | ||
--- | ||
|
||
## Overview | ||
|
||
Frequent asked questions are collected in this section. |
44 changes: 44 additions & 0 deletions
44
i18n/en/docusaurus-plugin-content-docs/current/faq/http/fileserver.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# File Server | ||
|
||
## How to provide file services using go-zero? | ||
|
||
You can add file service capabilities to a `restful` service through `go-zero` using `rest.WithFileServer(path, dir)`. | ||
|
||
Here is an example code: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/zeromicro/go-zero/rest" | ||
) | ||
|
||
func main() { | ||
// There are files in the `html` directory that need to be provided externally, | ||
// for example, a file named `index.html`, which can be accessed via the path `/static/index.html`. | ||
server := rest.MustNewServer(rest.RestConf{ | ||
Host: "localhost", | ||
Port: 4000, | ||
}, rest.WithFileServer("/static", "html")) | ||
defer server.Stop() | ||
|
||
server.AddRoute(rest.Route{ | ||
Method: http.MethodGet, | ||
Path: "/hello", | ||
Handler: helloHandler, | ||
}) | ||
|
||
server.Start() | ||
} | ||
|
||
func helloHandler(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte("Hello, World!")) | ||
} | ||
``` | ||
|
||
This is just an example and generally should not be used for production services. It may be considered when the production service is very simple, but it is not best practice. Typically, file serving would be handled by `nginx` or cloud storage. | ||
|
||
go-zero version: >= v1.7.0 |
60 changes: 60 additions & 0 deletions
60
i18n/en/docusaurus-plugin-content-docs/current/faq/log/fileconsole.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# How to Output Logs to Both File and Console in go-zero? | ||
|
||
To output logs to both a file and the console while using the go-zero framework, follow these steps to configure and write your code. | ||
|
||
**Steps:** | ||
|
||
1. **Create the configuration file `config.yaml`**: | ||
First, define a YAML file to configure the logging mode and encoding format. | ||
|
||
```yaml | ||
Mode: file | ||
Encoding: json | ||
``` | ||
2. **Write the main program `main.go`**: | ||
Use the following Go code to load the configuration file and set up logging to both a file and the console. | ||
|
||
```go | ||
package main | ||
import ( | ||
"os" | ||
"time" | ||
"github.com/zeromicro/go-zero/core/conf" | ||
"github.com/zeromicro/go-zero/core/logx" | ||
"github.com/zeromicro/go-zero/core/proc" | ||
) | ||
func main() { | ||
var c logx.LogConf | ||
conf.MustLoad("config.yaml", &c) // Load the configuration file | ||
logx.MustSetup(c) // Set up the logging configuration | ||
logx.AddWriter(logx.NewWriter(os.Stdout)) // Add console output | ||
for { | ||
select { | ||
case <-proc.Done(): // Check if the program needs to exit | ||
return | ||
default: | ||
time.Sleep(time.Second) | ||
logx.Info(time.Now()) // Log the current time | ||
} | ||
} | ||
} | ||
``` | ||
|
||
**Detailed Explanation**: | ||
|
||
- **Configuration File (`config.yaml`)**: | ||
- `Mode: file` specifies that logs should be output to a file. | ||
- `Encoding: json` specifies that the log format will be JSON. | ||
|
||
- **Main Program (`main.go`)**: | ||
- Use `conf.MustLoad` to load the configuration file. | ||
- Call `logx.MustSetup` to configure the logging system. | ||
- Use `logx.AddWriter` to add an additional logging target. Here, we add standard output (console). | ||
- In an infinite loop, record the current time every second. The `select` statement combined with `proc.Done()` allows for smooth program termination. | ||
|
||
By following the above configuration and code, you can achieve log output to both a file and the console in go-zero. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters