Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ftr: 3.0 lumberjack log sample #179

Merged
merged 2 commits into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .run/logger-client.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="logger-client" type="GoApplicationRunConfiguration" factoryName="Go Application" singleton="false">
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$" />
<envs>
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/logger/go-client/conf/log.yml" />
<env name="CONF_CONSUMER_FILE_PATH" value="$PROJECT_DIR$/logger/go-client/conf/client.yml" />
</envs>
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/logger/go-client/cmd/client.go" />
<package value="github.com/apache/dubbo-go-samples/logger/go-client/cmd" />
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
15 changes: 15 additions & 0 deletions .run/logger-server.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="logger-server" type="GoApplicationRunConfiguration" factoryName="Go Application" singleton="false">
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$" />
<envs>
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/logger/go-server/conf/log.yml" />
<env name="CONF_PROVIDER_FILE_PATH" value="$PROJECT_DIR$/logger/go-server/conf/server.yml" />
</envs>
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/logger/go-server/cmd/server.go" />
<package value="github.com/apache/dubbo-go-samples/logger/go-server/cmd" />
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
4 changes: 2 additions & 2 deletions .run/tracing-dubbo-go-client.run.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$" />
<envs>
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/tracing/dubbo/go-client/conf/log.yml" />
<env name="CONF_CONSUMER_FILE_PATH" value="$PROJECT_DIR$/tracing/dubbo/go-client/conf/client.yml" />
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/tracing/dubbo/go-client/profiles/dev/log.yml" />
<env name="CONF_CONSUMER_FILE_PATH" value="$PROJECT_DIR$/tracing/dubbo/go-client/profiles/dev/client.yml" />
</envs>
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/tracing/dubbo/go-client/cmd/client.go" />
Expand Down
4 changes: 2 additions & 2 deletions .run/tracing-dubbo-go-server.run.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$" />
<envs>
<env name="CONF_PROVIDER_FILE_PATH" value="$PROJECT_DIR$/tracing/dubbo/go-server/conf/server.yml" />
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/tracing/dubbo/go-server/conf/log.yml" />
<env name="CONF_PROVIDER_FILE_PATH" value="$PROJECT_DIR$/tracing/dubbo/go-server/profiles/dev/server.yml" />
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/tracing/dubbo/go-server/profiles/dev/log.yml" />
</envs>
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/tracing/dubbo/go-server/cmd/server.go" />
Expand Down
116 changes: 116 additions & 0 deletions logger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
## Log usage

The samples demonstrate how to configure dubbo-go logger using lumberjack

### Configuration

Output file settings

```yaml
lumberjackConfig:
# The name of the log file
filename: "logs.log"
# The maximum size of each log file length, the default is 100M
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The maximum size of each log file length, its unit is MiB. The default value is 100MiB.

maxSize: 1
# Maximum number of days to keep logs (only keep the logs of the most recent days)
maxAge: 3
# Only keep the most recent log files, used to control the size of the total log of the program
maxBackups: 5
# Whether to use local time, UTC time is used by default
localTime: true
# Whether to compress the log file, the compression method is gzip
compress: false
```

Log format and level settings

```yaml
zapConfig:
level: "debug"
development: false
disableCaller: false
disableStacktrace: false
sampling:
encoding: "console"

# encoder
encoderConfig:
messageKey: "message"
levelKey: "level"
timeKey: "time"
nameKey: "logger"
callerKey: "caller"
stacktraceKey: "stacktrace"
lineEnding: ""
levelEncoder: "capitalColor"
timeEncoder: "iso8601"
durationEncoder: "seconds"
callerEncoder: "short"
nameEncoder: ""

outputPaths:
- "stderr"
errorOutputPaths:
- "stderr"
initialFields:
```

If you do not want to output to a file, you can follow the rules below to set only zap

```yaml
level: "debug"
development: false
disableCaller: false
disableStacktrace: false
sampling:
encoding: "console"

# encoder
encoderConfig:
messageKey: "message"
levelKey: "level"
timeKey: "time"
nameKey: "logger"
callerKey: "caller"
stacktraceKey: "stacktrace"
lineEnding: ""
levelEncoder: "capitalColor"
timeEncoder: "iso8601"
durationEncoder: "seconds"
callerEncoder: "short"
nameEncoder: ""

outputPaths:
- "stderr"
errorOutputPaths:
- "stderr"
initialFields:
```

### Run

A cycle of 1000000 times is set on the go-client side, and the server will be called all the time to facilitate the viewing of the log output

```go
func main() {
config.Load()
time.Sleep(3 * time.Second)

for i := 0;i < 1000000; i ++ {
test()
}

}

func test() {
logger.Info("\n\n\nstart to test dubbo")
user := &pkg.User{}
err := userProvider.GetUser(context.TODO(), []interface{}{"A001"}, user)
if err != nil {
logger.Infof("error: %v\n", err)
os.Exit(1)
return
}
logger.Infof("response result: %v\n", user)
}
```
116 changes: 116 additions & 0 deletions logger/README_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
## 日志使用

该 samples 演示了如何使用 lumberjack 配置 dubbo-go logger

### 配置

输出文件设置

```yaml
lumberjackConfig:
# 写日志的文件名称
filename: "logs.log"
# 每个日志文件长度的最大大小,默认100M
maxSize: 1
# 日志保留的最大天数(只保留最近多少天的日志)
maxAge: 3
# 只保留最近多少个日志文件,用于控制程序总日志的大小
maxBackups: 5
# 是否使用本地时间,默认使用UTC时间
localTime: true
# 是否压缩日志文件,压缩方法gzip
compress: false
```

zap 日志格式和级别设置

```yaml
zapConfig:
level: "debug"
development: false
disableCaller: false
disableStacktrace: false
sampling:
encoding: "console"

# encoder
encoderConfig:
messageKey: "message"
levelKey: "level"
timeKey: "time"
nameKey: "logger"
callerKey: "caller"
stacktraceKey: "stacktrace"
lineEnding: ""
levelEncoder: "capitalColor"
timeEncoder: "iso8601"
durationEncoder: "seconds"
callerEncoder: "short"
nameEncoder: ""

outputPaths:
- "stderr"
errorOutputPaths:
- "stderr"
initialFields:
```

如果不希望输出到文件中,可以按照如下规则,仅设置 zap

```yaml
level: "debug"
development: false
disableCaller: false
disableStacktrace: false
sampling:
encoding: "console"

# encoder
encoderConfig:
messageKey: "message"
levelKey: "level"
timeKey: "time"
nameKey: "logger"
callerKey: "caller"
stacktraceKey: "stacktrace"
lineEnding: ""
levelEncoder: "capitalColor"
timeEncoder: "iso8601"
durationEncoder: "seconds"
callerEncoder: "short"
nameEncoder: ""

outputPaths:
- "stderr"
errorOutputPaths:
- "stderr"
initialFields:
```

### 运行

在 go-client 端设置了一个 1000000 次的循环,会一直调用 server 来方便查看日志的输出情况

```go
func main() {
config.Load()
time.Sleep(3 * time.Second)

for i := 0;i < 1000000; i ++ {
test()
}

}

func test() {
logger.Info("\n\n\nstart to test dubbo")
user := &pkg.User{}
err := userProvider.GetUser(context.TODO(), []interface{}{"A001"}, user)
if err != nil {
logger.Infof("error: %v\n", err)
os.Exit(1)
return
}
logger.Infof("response result: %v\n", user)
}
```
72 changes: 72 additions & 0 deletions logger/go-client/cmd/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"context"
"os"
"time"
)

import (
_ "dubbo.apache.org/dubbo-go/v3/cluster/cluster_impl"
_ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance"
_ "dubbo.apache.org/dubbo-go/v3/common/proxy/proxy_factory"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/common/logger"
_ "dubbo.apache.org/dubbo-go/v3/filter/filter_impl"
_ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo"
_ "dubbo.apache.org/dubbo-go/v3/registry/protocol"
_ "dubbo.apache.org/dubbo-go/v3/registry/zookeeper"

hessian "github.com/apache/dubbo-go-hessian2"
)

import (
"github.com/apache/dubbo-go-samples/helloworld/go-client/pkg"
)

var userProvider = new(pkg.UserProvider)

func init() {
config.SetConsumerService(userProvider)
hessian.RegisterPOJO(&pkg.User{})
}

// need to setup environment variable "CONF_CONSUMER_FILE_PATH" to "conf/client.yml" before run
func main() {
config.Load()
time.Sleep(3 * time.Second)

for i := 0;i < 1000000; i ++ {
test()
}

}

func test() {
logger.Info("\n\n\nstart to test dubbo")
user := &pkg.User{}
err := userProvider.GetUser(context.TODO(), []interface{}{"A001"}, user)
if err != nil {
logger.Infof("error: %v\n", err)
os.Exit(1)
return
}
logger.Infof("response result: %v\n", user)
}
Loading