服务端支持配置文件,所有的配置将会自动映射到配置对象中。配置对象如下:
// GrpcServerConfig is the configuration for server. type GrpcServerConfig struct { Address string // (optional) Address for server listening. Name string // (optional) Name for current service. Logger *glog.Logger // (optional) Logger for server. LogPath string // (optional) LogPath specifies the directory for storing logging files. LogStdout bool // (optional) LogStdout specifies whether printing logging content to stdout. ErrorStack bool // (optional) ErrorStack specifies whether logging stack information when error. ErrorLogEnabled bool // (optional) ErrorLogEnabled enables error logging content to files. ErrorLogPattern string // (optional) ErrorLogPattern specifies the error log file pattern like: error-{Ymd}.log AccessLogEnabled bool // (optional) AccessLogEnabled enables access logging content to file. AccessLogPattern string // (optional) AccessLogPattern specifies the error log file pattern like: access-{Ymd}.log } |
配置文件的自动读取逻辑与框架一致,详细介绍请参考章节:配置管理
一个完整的配置模板示例:
grpc: name: "demo" # 服务名称 address: ":8000" # 自定义服务监听地址 logPath: "./log" # 日志存储目录路径 logStdout: true # 日志是否输出到终端 errorLogEnabled: true # 是否开启错误日志记录 accessLogEnabled: true # 是否开启访问日志记录 errorStack: true # 当产生错误时,是否记录错误堆栈 # 日志扩展配置(参数日志组件配置) logger: path: "/var/log/" # 日志文件路径。默认为空,表示关闭,仅输出到终端 file: "{Y-m-d}.log" # 日志文件格式。默认为"{Y-m-d}.log" prefix: "" # 日志内容输出前缀。默认为空 level: "all" # 日志输出级别 stdout: true # 日志是否同时输出到终端。默认true rotateSize: 0 # 按照日志文件大小对文件进行滚动切分。默认为0,表示关闭滚动切分特性 rotateExpire: 0 # 按照日志文件时间间隔对文件滚动切分。默认为0,表示关闭滚动切分特性 rotateBackupLimit: 0 # 按照切分的文件数量清理切分文件,当滚动切分特性开启时有效。默认为0,表示不备份,切分则删除 rotateBackupExpire: 0 # 按照切分的文件有效期清理切分文件,当滚动切分特性开启时有效。默认为0,表示不备份,切分则删除 rotateBackupCompress: 0 # 滚动切分文件的压缩比(0-9)。默认为0,表示不压缩 rotateCheckInterval: "1h" # 滚动切分的时间检测间隔,一般不需要设置。默认为1小时 |
其中的日志配置与http server
一致,可以独立使用日志组件的配置项来配置grpc server
的日志。关于日志组件的配置,可参考该文档详细了解:日志组件-配置管理
在没有配置 |