Skip to content

Commit

Permalink
feat: add output to console faq
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Jul 17, 2024
1 parent 026a8d9 commit d138cb3
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/faq/http/fileserver.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ func helloHandler(w http.ResponseWriter, r *http.Request) {

这仅仅是个示例,一般不用做生产服务,或者当生产服务很简单的时候可以考虑使用,但不是最佳实践,一般会通过 `nginx` 或者云存储提供。

go-zero 版本:>= v1.7.0
> go-zero 版本:>= v1.7.0
64 changes: 64 additions & 0 deletions docs/faq/log/fileconsole.md
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
8 changes: 8 additions & 0 deletions i18n/en/docusaurus-plugin-content-docs/current/faq.md
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.
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
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.
8 changes: 8 additions & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,14 @@ module.exports = {
'faq/http/fileserver',
],
},
{
type: 'category',
label: '日志',
collapsed: false,
items: [
'faq/log/fileconsole',
],
},
],
contributing: [
'contributing',
Expand Down

0 comments on commit d138cb3

Please sign in to comment.