ESql日志配置

conf/esql.yml

logging: {

  # 默认日志格式, 各 handler 可自定义该格式。格式参考文档:docs.python.org/3/library/logging.html#logrecord-attributes
  format: '%(asctime)s %(levelname)s [%(name)s] %(process)d %(message)s',

  # 已经输出过的日志消息是否重复输出到 default logger(root.log)
  propagate: false

  # ...
}

下文中的配置都包含在 logging: {...} 中

输出格式(format)

标准 输出字段定义 (可用于所有模块的日志输出):

asctime 时间字符串
levelname 日志级别(DEBUG、INFO、ERROR、...)
name 模块名(路径)
process 进程号
message 日志信息
... ...

ESql特有字段定义(只可用于定义ESql程序的日志输出,不可用于定义第三方模块的日志输出):

client 发起SQL请求的客户端的IP地址
transaction 事务编号(每个SQL请求对应一个事务编号)

日志服务器

# 使用日志服务器(基于UDP)可避免多进程引起的日志丢失现象。
server: {
  # 为防止端口冲突建议配置为UNIX域套接字(AF_UNIX)
  # 如果日志服务器要和ESql(uwsgi)分开部署,需配置为UNIX网络套接字(AF_INET)如:address: 'xx.xx.xx.xx:端口'
  address: run/logger.sock

  # Mac环境由于UDP最大长度限制的过小,需要配置 /etc/sysctl.conf(如xxx=65535),并执行如下命令以便生效(sudo -s):
  # chown root:wheel /etc/sysctl.conf; chmod 0644 /etc/sysctl.conf
  # sysctl -w net.local.dgram.maxdgram=65535; sysctl -w net.local.dgram.recvspace=131070      # AF_UNIX
  # sysctl -w net.inet.udp.maxdgram=65535; sysctl -w net.inet.udp.recvspace=393448            # AF_INET
}

Class 定义

ESql定义有如下三类日志,在下文的 handler 中使用(handler中可对选用的class进行自定义,如:colour: false)

stream 屏幕输出(开发和调试时用)
socket_rotating_size 按文件固定大小输出,默认配置为每个日志文件50MB,日志文件最多保存个数为10个
socket_rotating_time 按时间间隔输出,默认配置为每天一个日志文件,日志文件最多保存个数为10个
# all parameter can redefine in each handler
class: {
  # screen output
  stream: {
    colour: true              # M$ windows not support
  },
  socket_rotating_size: {
    max_size: 1024*1024*50,    # MB (byte / 1024 / 1024)
    backup_count: 10           # reserve log file count
  },
  socket_rotating_time: {
    when: d,                   # timing unit   s:second   m:minute   h:hour   d:day    w:week
    interval: 1,
    backup_count: 10          # reserve log file count
  }
}

Handler 定义

ESql默认定义了如下三个的日志,可根据实际需求自行增删、修改。 如果要自定义root的format,不可在其内使用 %(client)s 和 %(transaction)d。

log/esql.log ESql日志
log/esql-debug.log ESql详细日志
log/root.log 其它(第三方组件)日志
handler: {
  esql: {
    level: info,
    class: socket_rotating_size,
    path: log/esql.log,             # can use absolute or relative path, the latter use configure app.workspace
    format: '%(asctime)s %(levelname)s [%(client)s] %(process)d_%(transaction)d %(message)s'    # %(name)s %(thread)d
  },
  esql_debug: {
    level: debug,
    class: socket_rotating_size,
    path: log/esql-debug.log,       # can use absolute or relative path, the latter use configure app.workspace
    format: '%(asctime)s %(levelname)s [%(client)s] %(process)d_%(transaction)d %(message)s'    # %(name)s %(thread)d
  },
  root: {
    class: socket_rotating_size,
    path: log/root.log              # can use absolute or relative path, the latter use configure app.workspace
  }
}

Logger 定义

logger 定义了哪些模块的日志输出到哪些文件中去,没定义模块的则输出到 default 定义的日志文件中。

logger: {
  default: {
    handler: root stream                       # stream must on the last, otherwise the output will be chaos
  },
  esql: {
    handler: esql esql_debug stream            # stream must on the last, otherwise the output will be chaos
  },
  eweb: {
    handler: esql esql_debug stream            # stream must on the last, otherwise the output will be chaos
  },
  elasticsearch: {
    level: warn,
    handler: root stream                       # stream must on the last, otherwise the output will be chaos
  }
}