文章目录
  1. 1. Logging
    1. 1.1. Log Format
    2. 1.2. Configuration
  2. 2. Logging settings.
    1. 2.1. Console Logging
    2. 2.2. File Logging
    3. 2.3. Syslog Logging
    4. 2.4. 参数介绍
  3. 3. Server
    1. 3.1. applicationConnectors
    2. 3.2. adminConnectors
  4. 4. 参考

Dropwizard每一个Application子类都有一个类型参数,也就是Configuration子类。当我们的应用运行server配置命令时,Dropwizard会解析YAML配置文件,然后通过映射来创建一个Configuration的实例。

YAML配置一般存在于项目中的.yml活着.yaml文件中,一个配置文件实例如下:

template: Hello, %s!

defaultName: ${DW_DEFAULT_NAME:-Stranger}

# Database settings.
database:

  # the name of your JDBC driver
  driverClass: org.h2.Driver

  # the username
  user: sa

  # the password
  password: sa

  # the JDBC URL
  url: jdbc:h2:./target/example

# use the simple server factory if you only want to run on a single port
#server:
#  type: simple
#  connector:
#    type: http
#    port: 8080

server:
#  softNofileLimit: 1000
#  hardNofileLimit: 1000
  applicationConnectors:
    - type: http
      port: 8080
    - type: https
      port: 8443
      keyStorePath: example.keystore
      keyStorePassword: example
      validateCerts: false
# this requires the alpn-boot library on the JVM's boot classpath
#    - type: spdy3
#      port: 8445
#      keyStorePath: example.keystore
#      keyStorePassword: example
#      validateCerts: false
  adminConnectors:
    - type: http
      port: 8081
    - type: https
      port: 8444
      keyStorePath: example.keystore
      keyStorePassword: example
      validateCerts: false

# Logging settings.
logging:

  # The default level of all loggers. Can be OFF, ERROR, WARN, INFO, DEBUG, TRACE, or ALL.
  level: INFO

  # Logger-specific levels.
  loggers:

    # Sets the level for 'com.example.app' to DEBUG.
    com.example.app: DEBUG

    org.hibernate.SQL: ALL

  appenders:
    - type: console
    - type: file
      threshold: DEBUG
      logFormat: "%-6level [%d{HH:mm:ss.SSS}] [%t] %logger{5} - %X{code} %msg %n"
      currentLogFilename: /tmp/application.log
      archivedLogFilenamePattern: /tmp/application-%d{yyyy-MM-dd}.log
      archivedFileCount: 7
      timeZone: UTC

# the key needs to match the suffix of the renderer
viewRendererConfiguration:
    .ftl:
        strict_syntax: yes
        whitespace_stripping: yes

下面我们就介绍一下其中我们项目中经常使用到的配置参数。

Logging

Dropwizard使用Logback记录日志,同时提供了slf4j的实现,甚至通过Logback可以使用所有的java.util.logging, Log4j, and Apache Commons Logging。

Log Format

TRACE [2010-04-06 06:42:35,271] com.example.dw.Thing: Contemplating doing a thing.
DEBUG [2010-04-06 06:42:35,274] com.example.dw.Thing: About to do a thing.

可以通过tail和grep命令过滤Log,例如:

tail -f dw.log | grep '^WARN'

Configuration

可以在YAML配置文件中进行配置

Logging settings.

logging:

  # The default level of all loggers. Can be OFF, ERROR, WARN, INFO, DEBUG, TRACE, or ALL.
  level: INFO

  # Logger-specific levels.
  loggers:

    # Overrides the level of com.example.dw.Thing and sets it to DEBUG.
    "com.example.dw.Thing": DEBUG

Console Logging

By default, Dropwizard applications log INFO and higher to STDOUT。

日志可以输出到控制台:

logging:
  appenders:
    - type: console
      threshold: WARN
      target: stderr

File Logging

也可以输出到文件中:

logging:

  appenders:
    - type: file
      # The file to which current statements will be logged.
      currentLogFilename: ./logs/example.log

      # When the log file rotates, the archived log will be renamed to this and gzipped. The
      # %d is replaced with the previous day (yyyy-MM-dd). Custom rolling windows can be created
      # by passing a SimpleDateFormat-compatible format as an argument: "%d{yyyy-MM-dd-hh}".
      archivedLogFilenamePattern: ./logs/example-%d.log.gz

      # The number of archived files to keep.
      archivedFileCount: 5

      # The timezone used to format dates. HINT: USE THE DEFAULT, UTC.
      timeZone: UTC

Syslog Logging

Dropwizard can also log statements to syslog:

logging:

  appenders:
    - type: syslog
      # The hostname of the syslog server to which statements will be sent.
      # N.B.: If this is the local host, the local syslog instance will need to be configured to
      # listen on an inet socket, not just a Unix socket.
      host: localhost

      # The syslog facility to which statements will be sent.
      facility: local0

参数介绍

  1. Level: Logback输出级别
  2. loggers: 特定日志配置
  3. appenders: 输出目标配置,可以组合多个。

Server

参考最初给出的配置文件例子,我们主要介绍下面几个配置参数:

applicationConnectors

处理应用请求的connectors集合。

adminConnectors

处理admin请求的connectors 集合。例如:

  1. POST http://dw.example.com:8081/tasks/gc
  2. GET http://dw.example.com:8081/threads
  3. GET http://dw.example.com:8081/healthcheck

参考

  1. Dropwizard Core
  2. Dropwizard Configuration Reference
文章目录
  1. 1. Logging
    1. 1.1. Log Format
    2. 1.2. Configuration
  2. 2. Logging settings.
    1. 2.1. Console Logging
    2. 2.2. File Logging
    3. 2.3. Syslog Logging
    4. 2.4. 参数介绍
  3. 3. Server
    1. 3.1. applicationConnectors
    2. 3.2. adminConnectors
  4. 4. 参考