Introduction

The server supports configuration files, and all configurations will be automatically mapped to the configuration object. The configuration object is as follows:

  1. // GrpcServerConfig is the configuration for server.
  2. type GrpcServerConfig struct {
  3. Address string // (optional) Address for server listening.
  4. Name string // (optional) Name for current service.
  5. Logger *glog.Logger // (optional) Logger for server.
  6. LogPath string // (optional) LogPath specifies the directory for storing logging files.
  7. LogStdout bool // (optional) LogStdout specifies whether printing logging content to stdout.
  8. ErrorStack bool // (optional) ErrorStack specifies whether logging stack information when error.
  9. ErrorLogEnabled bool // (optional) ErrorLogEnabled enables error logging content to files.
  10. ErrorLogPattern string // (optional) ErrorLogPattern specifies the error log file pattern like: error-{Ymd}.log
  11. AccessLogEnabled bool // (optional) AccessLogEnabled enables access logging content to file.
  12. AccessLogPattern string // (optional) AccessLogPattern specifies the error log file pattern like: access-{Ymd}.log
  13. }

The logic for automatic reading of configuration files is consistent with the framework. For detailed information, please refer to the section: Configuration

Configuration Template

An example of a complete configuration template:

  1. grpc:
  2. name: "demo" # Service name
  3. address: ":8000" # Custom service listening address
  4. logPath: "./log" # Log storage directory path
  5. logStdout: true # Whether to output logs to the terminal
  6. errorLogEnabled: true # Whether to enable error logging
  7. accessLogEnabled: true # Whether to enable access logging
  8. errorStack: true # Whether to log error stacks when errors occur
  9. # Log extension configuration (parameter log component configuration)
  10. logger:
  11. path: "/var/log/" # Log file path. Default is empty, indicating disabled, output to terminal only
  12. file: "{Y-m-d}.log" # Log file format. Default is "{Y-m-d}.log"
  13. prefix: "" # Prefix for log content output. Default is empty
  14. level: "all" # Log output level
  15. stdout: true # Whether to output logs to terminal simultaneously. Default is true
  16. rotateSize: 0 # Rotate files based on log file size. Default is 0, indicating disabled
  17. rotateExpire: 0 # Rotate files based on time intervals. Default is 0, indicating disabled
  18. rotateBackupLimit: 0 # Limit backup files based on the number of split files, valid when rotation is enabled. Default is 0, meaning no backup, delete when split
  19. rotateBackupExpire: 0 # Clean up split files based on the expiration period, valid when rotation is enabled. Default is 0, meaning no backup, delete when split
  20. rotateBackupCompress: 0 # Compression ratio for rotating files (0-9). Default is 0, indicating no compression
  21. rotateCheckInterval: "1h" # Time interval for rotation checks, usually no need to set. Default is 1 hour

The log configuration here is consistent with the http server and can use independent log component configuration items to configure the grpc server logs. For detailed information about the log component configuration, please refer to this document: Logging - Configuration

In case the address is not configured, the grpc server will start using all ip addresses of the local network card with a random free port (default configuration :0). If you want to specify the ip but use a random free port to start the grpc server, you can configure the address using the format ip:0, for example: 192.168.1.1:0, 10.0.1.1:0.