Skip to content

Commit

Permalink
Merge pull request #379 from Cognifide/feature/378-request-logging
Browse files Browse the repository at this point in the history
Feature/378 request logging
  • Loading branch information
marcinczeczko authored Jan 25, 2018
2 parents 5eca5d4 + 327e6ac commit dc865af
Show file tree
Hide file tree
Showing 32 changed files with 641 additions and 178 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ List of changes that are finished but not yet released in any final version.
- [PR-372](https://github.com/Cognifide/knotx/pull/372) - Added cache for compiled Handlebars snippets
- [PR-374](https://github.com/Cognifide/knotx/pull/374) - Enable keepAlive connection in http client options
- [PR-380](https://github.com/Cognifide/knotx/pull/380) - Upgrade jsoup to 1.11.2
- [PR-379](https://github.com/Cognifide/knotx/pull/379) - Added access logging capabilities to the Knotx HTTP Server. Establish standard configuration of logback logger.

## Version 1.1.2
- [PR-318](https://github.com/Cognifide/knotx/pull/318) - Knot.x returns exit code `30` in case of missing config
Expand Down
1 change: 1 addition & 0 deletions documentation/src/main/DocumentationTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "wiki/Mocks.md"
#include "wiki/KnotxDeployment.md"
#include "wiki/KnotxTuning.md"
#include "wiki/Logging.md"
#include "wiki/Dependencies.md"
#include "wiki/FAQ.md"
#include "wiki/UpgradeNotes.md"
1 change: 1 addition & 0 deletions documentation/src/main/wiki/Home.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ User documentation is available at [http://knotx.io](http://knotx.io/).
* [[Mocks|Mocks]]
* [[Knot.x Deployment|KnotxDeployment]]
* [[Knot.x Tuning|KnotxTuning]]
* [[Logging|Logging]]
* [[Performance|PerformanceTests]]
* [[Performance Tests Methodology|PerformanceTestsMethodology]]
* [[Performance Tests Summary|PerformanceTestsSummary]]
Expand Down
178 changes: 178 additions & 0 deletions documentation/src/main/wiki/Logging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
# Logging
By default Knot.x picks up the logger configuration from its default location for the system (e.g. classpath:logback.xml),
but you can set the location of the config file through `logback.configurationFile` system property.
```
java -Dlogback.configurationFile=/path/to/logback.xml -jar ...
```
Knot.x core provides preconfigured three log files:
- `knotx.log` that logs all **ERROR** messages from the Netty & Vert.x and **INFO** messages from Knot.x application. Enabled by default on Knot.x standalone
- `knotx-access.log` that logs all HTTP requests/responses to the Knot.x HTTP Server. Enabled by default on Knot.x standalone
- `knotx-netty.log` that logs all network activity (logged by the Netty). Not enabled on Knot.x standalone. See [[Log network activity|#log-network-activity]] on how to enable it.

All logs are configured by default:
- To log both to file and console
- Log files are rolled over every day or if the log file exceeds 10MB
- Rolled files are automatically compressed
- History log files are kept forever

A default configuration can be overriden in order to meet your log policy. See [[Configure logback|#configure-logback]] for details.

## Configure Logback
Knot.x provides a default set of logback configurations that you can include, if you just want to set levels, or create your own specific loggers, e.g.

```xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<include resource="io/knotx/logging/logback/defaults.xml"/>
<include resource="io/knotx/logging/logback/console-appender.xml"/>
<include resource="io/knotx/logging/logback/file-appender.xml"/>

<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>

<logger name="io.knotx" level="TRACE"/>
</configuration>
```
Will create console & file logger for Knot.x logs. Besides that there are other includes that brings new logs, these are:
- `io/knotx/logging/logback/access.xml` - access log
- `io/knotx/logging/logback/netty.xml` - network activity logs

All those configurations uses useful System properties which the Logback takes care of creating for you. These are:
- `${LOG_PATH}` - represents a directory for log files to live in, for knotx, access & netty logs. It's set with the value from `LOG_PATH` System property, or from your own logback.xml file. If none of these specified, logs to the current working directory `/logs` subfolder.
- `${KNOTX_LOG_FILE}`, `${ACCESS_LOG_FILE}`, `${NETTY_LOG_FILE}` - Ignores `LOG_PATH` and use that property to specify actual location for the knotx, access & netty log files (e.g. `/var/logs/knotx.log`, or `/var/logs/access.log`)
- `${KNOTX_LOG_DATEFORMAT_PATTERN}` - Allows to specify a different date-time format for log entries in `knotx.log` and `knotx-netty.log` files only. If not specified, `yyyy-MM-dd HH:mm:ss.SSS` is used.
- `${CONSOLE_KNOTX_LOG_PATTERN}` - Allows to override a default log pattern for console logging.
- `${FILE_KNOTX_LOG_PATTERN}`, `${FILE_ACCESS_LOG_PATTERN}`, `${FILE_NETTY_LOG_PATTERN}` - Allows to override a default log pattern for knotx, access & netty logs.
- `${KNOTX_LOG_FILE_MAX_SIZE}`, `${ACCESS_LOG_FILE_MAX_SIZE}`, `${NETTY_LOG_FILE_MAX_SIZE}` - Allows to define a maximum size of knotx, access & netty log files. If not specified, a max size is `10MB`
- `${KNOTX_LOG_FILE_MAX_HISTORY}`, `${ACCESS_LOG_FILE_MAX_HISTORY}`, `${NETTY_LOG_FILE_MAX_HISTORY}` - Allows to define a maximum amount of archived log files kept in the log folder (for knotx, access & netty logs). If not specified, it keeps files forever.

See the [Knot.x core logback settings](https://github.com/Cognifide/knotx/blob/master/knotx-core/src/main/resources/io/knotx/logging/logback/) configuration files for details.

## Configure logback for file only output
In a production system, you want to disable console logging and write output only to a file both for knotx & access logs.
You need to create a custom `logback.xml` that imports `file-appender.xml` but not `console-appender.xml`:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="io/knotx/logging/logback/defaults.xml" />
<include resource="io/knotx/logging/logback/file-appender.xml" />
<include resource="io/knotx/logging/logback/access.xml" />

<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
```

**NOTE: Do not forgot to specify usage of your custom logback file through `-Dlogback.configurationFile` system property.**

Additionally, you want to specify a location of log files in your file system. And roll over policy to keep last 30 archived log files.
You can do this, through your custom logback file:
```xml
<configuration>
<property name="LOG_PATH" value="/path/to/logs"/>
<property name="KNOTX_LOG_FILE_MAX_HISTORY" value="30"/>
<property name="ACCESS_LOG_FILE_MAX_HISTORY" value="30"/>

<include resource="io/knotx/logging/logback/defaults.xml" />
...
</configuration>
```
Or, you can provide those settings through System properties:
```
-DLOG_PATH=/path/to/logs -DKNOTX_LOG_FILE_MAX_HISTORY=30 -DACCESS_LOG_FILE_MAX_HISTORY=30
```
Or, even you can mix both approaches. So define default settings inside logback, and configure to respect new System properties you can use to override
```xml
<configuration>
<property name="LOG_PATH" value="${my.logs:-/path/to/logs}"/>
<property name="KNOTX_LOG_FILE_MAX_HISTORY" value="${my.logs.history:-30}"/>
<property name="ACCESS_LOG_FILE_MAX_HISTORY" value="${my.logs.history:-30}"/>

<include resource="io/knotx/logging/logback/defaults.xml" />
...
</configuration>
```
And your System property, that will set different log path, and max history for both log files to `100`:
```
-Dmy.logs=/other/path -Dmy.logs.history=100
```
As you can see, Logback logger brings a tremendous amount of possibilities how to configure your logs.
It's impossible to present here all the possibilities, so the best way would be to study [Logback Documentation](https://logback.qos.ch/manual/index.html).

## Log network activity
Network activity (logged by Netty) logger settings are provided by the Knot.x core. In order to log it, you need to use your custom logback.xml file and configure it as follows
```xml
<configuration>
<!-- Your properties is required -->

<!-- Knotx & access logs -->
<include resource="io/knotx/logging/logback/defaults.xml" />
<include resource="io/knotx/logging/logback/file-appender.xml" />
<include resource="io/knotx/logging/logback/access.xml" />

<!-- Add netty network activity logger -->
<include resource="io/knotx/logging/logback/netty.xml" />

<!-- other logger settings -->
</configuration>
```
Additionally, in your knot.x json configuration file, you need to enable logging network activity. You can enable this both for HTTP server as well as HTTP clients used.
- To enable it for server, just overwrite KnotxServer configuration:
```json
"config": {
"serverOptions": {
"logActivity": true
}
}
```
- To enable it for HTTP Clients, just overwrite any or all service adapter configurations:
```json
"config": {
"clientOptions": {
"logActivity": true
}
}
```

## Configure logback to log my specific package
If you added your own Knot's, Adapters or any other extension to the Knot.x you want to have this information to be logged in your log files.
```xml
<configuration>
<!-- Your properties is required -->

<!-- Knotx & access logs -->
<include resource="io/knotx/logging/logback/defaults.xml" />
<include resource="io/knotx/logging/logback/file-appender.xml" />
<include resource="io/knotx/logging/logback/access.xml" />

<!-- other logger settings -->

<!-- project specific logger -->
<logger name="com.example.extension" level="INFO">
<appender-ref ref="FILE" />
</logger>
</configuration>
```
- Add `logger` for your package or class, define a level of logs
- Specify `FILE` appender as a logs target.
Your logs will appear in the `knotx.log`

However, you might wanted to log your package logs into a separate file, to not polute `knotx.log`.
- create your own file appender (see `io/knotx/logging/logback/file-appender.xml` as an example) with the name e.g. `MY_FILE`
- bind your logger to `MY_FILE` appender
- set logger to `additivity="false"`, so your logs will go just to your new file. If specified to `true` logs will go to the parent logger into `knotx.log` files too.

```xml
<appender name="MY_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- Appender settings -->
</appender>

<!-- project specific logger -->
<logger name="com.example.extension" level="INFO" additivity="false">
<appender-ref ref="MY_FILE" />
</logger>
```
38 changes: 38 additions & 0 deletions documentation/src/main/wiki/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Main server options available.
| `csrf` | `KnotxCSRFConfiguration` | | Configuration of the CSRF tokens |
| `defaultFlow` | `KnotxFlowConfiguration` | &#10004; | Configuration of [[default Knot.X routing|KnotRouting]] |
| `customFlow` | `KnotxFlowConfiguration` | | Configuration of [[Gateway Mode|GatewayMode]] |
| `accessLog` | `AccessLogConfiguration` | | Configuration of the KnotxServer access log |

### KnotxServerCustomHeader options
Name | Type | Mandatory | Description |
Expand Down Expand Up @@ -214,6 +215,15 @@ The `repositories`, `splitter` and `assembler` verticles are specific to the def
| `address` | `String` | &#10004; | Event bus address of the **Knot** verticle |
| `onTransition` | `KnotRouteEntry` | | Describes routing to addresses of other Knots based on the transition trigger returned from current Knot.<br/>`"onTransition": { "go-d": {}, "go-e": {} }` |


### AccessLogConfiguration options
| Name | Type | Mandatory | Description |
|-------:|:-------:|:-------: |-------|
| `enabled` | `boolean` | | Enable/Disable access log. Default is `true` |
| `immediate` | `boolean` | | Log before request or after. Default is `false` - log after request |
| `format` | `String` | | Format of the access log. Allowed valueds are `DEFAULT`, `SHORT`, `TINY`. See [[Configure Access Log|#configure-access-log]]. Default format is `DEFAULT` |


### Vert.x HTTP Server configurations

Besides Knot.x specific configurations as mentioned above, the `config` field might have added Vert.x configurations related to the HTTP server.
Expand Down Expand Up @@ -345,3 +355,31 @@ for eventubs requests that come from `KnotxServer`.
}
}
```

### Configure access log
Knot.x uses a default Logging handler from the Vert.x web distribution that allows to log all incomming requests to the Http server.
It supports three log line formats that are:
- DEFAULT that tries to log in a format similar to Apache log format (APACHE/NCSA COMBINED LOG FORMAT) as in the example
`127.0.0.1 - - [Tue, 23 Jan 2018 14:16:34 GMT] "GET /content/local/simple.html HTTP/1.1" 200 2963 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"`
- SHORT
`127.0.0.1 - GET /content/local/simple.html HTTP/1.1 200 2963 - 19 ms`
- TINY
`GET /content/local/simple.html 200 2963 - 24 ms`

By default access log is enabled with a `DEFAULT` format. If you want to change it, just add access logging section on the KnotxServer configuration in your application.json config file :
```json
{
"config": {
"knotx:io.knotx.KnotxServer": {
"options": {
"config": {
"accessLog": {
"format": "TINY"
}
}
}
}
}
}
```
In order to configure logger for access log, see [[Logging|Logging]].
4 changes: 3 additions & 1 deletion documentation/src/main/wiki/UpgradeNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ For more details see documentation sections in [[Server|Server#vertx-event-bus-d
in order to receive errors stack traces.
- [PR-369](https://github.com/Cognifide/knotx/pull/369) - Better support for SSL for Repository Connector. Please check the documentation of [[HttpRepositoryConnector|HttpRepositoryConnector#how-to-configure-ssl-connection-to-the-repository]] for details of how to setup SSL connection.
- [PR-372](https://github.com/Cognifide/knotx/pull/372) - Added cache for compiled Handlebars snippets, you may configure it in Handlebars config, see more in [[Handlebars Knot docs|HandlebarsKnot#how-to-configure]].
- [PR-374](https://github.com/Cognifide/knotx/pull/374) - Enable keepAlive connection in http client options. This is important fix and we recommend to update your existing configuration of any http client and enable `keepAlive` option.
- [PR-374](https://github.com/Cognifide/knotx/pull/374) - Enable keepAlive connection in http client options. This is important fix and we recommend to update your existing configuration of any http client and enable `keepAlive` option.
- [PR-379](https://github.com/Cognifide/knotx/pull/379) - Added access logging capabilities to the Knotx HTTP Server. Establish standard configuration of logback logger. Check [[Configure Access Log|Server#configure-access-log]] to see how to configure access logs, and [[Logging|Logging]] to see how loggers can be configured to log to console, files for knot.x, access & netty logs.


## Version 1.1.2
- [PR-335](https://github.com/Cognifide/knotx/pull/335) - Added support for HttpServerOptions on the configuration level.
Expand Down
1 change: 1 addition & 0 deletions documentation/src/main/wiki/_Sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* [[Mocks|Mocks]]
* [[Knot.x Deployment|KnotxDeployment]]
* [[Knot.x Tuning|KnotxTuning]]
* [[Logging|Logging]]
* [[Performance|PerformanceTests]]
* [[Performance Tests Methodology|PerformanceTestsMethodology]]
* [[Performance Tests Summary|PerformanceTestsSummary]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,14 @@
-->
<configuration>
<include resource="io/knotx/logging/logback/defaults.xml" />
<include resource="io/knotx/logging/logback/console-appender.xml" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>

<logger name="io.knotx.action.http" level="INFO"
additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<root level="ERROR">
<appender-ref ref="STDOUT"/>
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>

<logger name="io.knotx" level="TRACE">
<appender-ref ref="CONSOLE"/>
</logger>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,14 @@
-->
<configuration>
<include resource="io/knotx/logging/logback/defaults.xml" />
<include resource="io/knotx/logging/logback/console-appender.xml" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>

<logger name="io.knotx.adapter.service.http" level="TRACE"
additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<root level="ERROR">
<appender-ref ref="STDOUT"/>
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>

<logger name="io.knotx" level="TRACE">
<appender-ref ref="CONSOLE"/>
</logger>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import io.vertx.core.Launcher;
import io.vertx.core.impl.launcher.commands.ExecUtils;
import io.vertx.core.json.JsonObject;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class LogbackLauncher extends Launcher {

Expand All @@ -26,7 +28,7 @@ public class LogbackLauncher extends Launcher {
*/
public static final int KNOTX_MISSING_OR_EMPTY_CONFIGURATION_EXIT_CODE = 30;

public static void main(String[] args) {
public static void main(String[] args){
System.setProperty("vertx.logger-delegate-factory-class-name",
"io.vertx.core.logging.SLF4JLogDelegateFactory");
new LogbackLauncher().dispatch(args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2016 Cognifide Limited
Licensed 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.
-->
<!--
File appender logback configuration provided for import
-->

<included>
<appender name="ACCESS" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_ACCESS_LOG_PATTERN}</pattern>
</encoder>
<file>${ACCESS_LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${ACCESS_LOG_FILE}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<maxFileSize>${ACCESS_LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
<maxHistory>${ACCESS_LOG_FILE_MAX_HISTORY:-0}</maxHistory>
</rollingPolicy>
</appender>
</included>
Loading

0 comments on commit dc865af

Please sign in to comment.