You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+41-71Lines changed: 41 additions & 71 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,87 +26,57 @@ You must add the following to the list of appenders in your `log4js.json` file:
26
26
27
27
```
28
28
{
29
-
"type": "log4js-logmet-appender",
30
-
"options": {
31
-
"level": "INFO"
32
-
}
29
+
"type": "log4js-logmet-appender"
33
30
}
34
31
```
35
-
You may substitute `INFO` with your own level above (for ex: `WARN`, `ERROR`, etc). Please check [log4js](https://www.npmjs.com/package/log4js) documentation for level based filtering since there might be some differences between versions.
36
32
37
-
To use custom fields to send logmet, you should define `eventMap` object under `options`. If it is not defined then default mapping is used.
33
+
Using this configuration log messages to logmet will contain the following fields:
34
+
-`component`: value of the environment variable `log4js_logmet_component`
35
+
-`host-ip`: value of the environmental variable `CF_INSTANCE_IP`
36
+
-`instance-id`: value of the environmental variable `CF_INSTANCE_INDEX`
37
+
-`loglevel`: logging level (eg. `INFO`)
38
+
-`logtime`: time message was logged in ISO string format (eg. `2011-10-05T14:48:00.000Z`)
39
+
-`message`: log message/data, data array elements are concatenated with ` | ` delimeter
38
40
39
-
Default Event object (object sending to Logmet) consists of `component`, `host-ip`, `instance-id`, `loglevel`, `logtime`, `message` values.
41
+
### Custom Fields
40
42
41
-
For custom fields, values can be map are given below:
42
-
-`component`: The name of your component / service.
43
-
-`host-ip`: The cloudfoundary ip defined as environmental variable CF_INSTANCE_IP
44
-
-`instance-id`: The cloudfounrday index defined as environmental variable CF_INSTANCE_INDEX
-`logtime`: Logging time as ISO string format (exp. 2011-10-05T14:48:00.000Z)
47
-
-`message`: Log message based on log4js configuration. If log data is multiple string, then is is merged with ` | ` delimiter.
48
-
-`process.*` : To reach variables from `process` within two layer. (exp. process.pid, process.env.USER)
49
-
-`data.*` : To reach variables from optional `object` type from log data within two layer. This is useful, if you want to pass a custom object and use the value in your logs withing a custom field. (exp. You can pass `{clientId: '123'}` object to log and map it as `CLIENT_ID: data.clientId`)
43
+
To use custom fields, you should define an `options.fields` object.
44
+
Note: If a `fields` object is defined, none of the default fields will be sent.
50
45
51
-
Example `log4js.json` file given below (for log4js version 0.6.38 and below).
52
-
```
53
-
{
54
-
"appenders": [
55
-
{
56
-
"type": "console",
57
-
"layout": { "type": "coloured" }
58
-
},
59
-
{
60
-
"type": "logLevelFilter",
61
-
"level": "INFO",
62
-
"appender": {
63
-
"type": "log4js-logmet-appender",
64
-
"options": {
65
-
"eventMap": {
66
-
"TIMESTAMP": "logtime",
67
-
"PRIORITY": "loglevel",
68
-
"MESSAGE": "message",
69
-
"SERVICE": "component",
70
-
"_PID": "process.pid",
71
-
"USER": "process.env.USER",
72
-
"CUSTOM_ENV": "process.env.CUSTOM_VARIABLE"
73
-
"TEST": "data.test",
74
-
"TEST12": "data.test1.test2"
75
-
}
76
-
}
77
-
}
78
-
}
79
-
],
80
-
"replaceConsole": true
81
-
}
82
-
```
83
-
And example `debug` logging call;
84
-
```
85
-
var log4js = require('log4js');
86
-
log4js.configure('/path/to/log4js.json');
87
-
var logger = log4js.getLogger();
88
-
logger.level = 'debug';
89
-
const objectToTest = { test: 'test 1 is good', test1: { test2: 'test 2 is better' } };
This example prints coloured console log and filters `INFO` levels for `log4js-logmet-appender` appender.
93
-
`eventMap` determines the custom mapping for the object sending logmet. Values will be replaced based on event mapping and will send to logmet.
46
+
You can choose names for the log level (`"level_field_name"`), timestamp (`"timestamp_field_name"`) and data/message (`"data_field_name"`) fields. You may also add more custom static fields (under the `"static_fields"` object), fields that have node `process` object properties as values (under the `"process_fields"` object), and fields that have environment variables as values (under the `"env_fields"` object).
94
47
95
-
Example data to be sent to logmet based on given `log4js.json`
48
+
Example with custom fields:
96
49
```
97
-
{
98
-
"TIMESTAMP": "2011-10-05T14:48:00.000Z",
99
-
"PRIORITY": "INFO",
100
-
"MESSAGE": "Your log message",
101
-
"SERVICE": "name of your component",
102
-
"_PID": 1234,
103
-
"USER": "Username of the process",
104
-
"CUSTOM_ENV": "Environmental value defined as CUSTOM_VARIABLE",
105
-
"TEST": "test 1 is good",
106
-
"TEST12": "test 2 is better"
50
+
{
51
+
"type": "log4js-logmet-appender",
52
+
"options": {
53
+
"fields": {
54
+
"level_field_name": "PRIORITY",
55
+
"timestamp_field_name": "TIMESTAMP",
56
+
"data_field_name": "MESSAGE"
57
+
58
+
"static_fields": {
59
+
"COMPONENT": "otc-api"
60
+
},
61
+
62
+
"process_fields": {
63
+
"PROCESS_ID": "pid"
64
+
},
65
+
66
+
"env_fields": {
67
+
"INSTANCE_INDEX": "CF_INSTANCE_INDEX"
68
+
}
69
+
}
70
+
}
107
71
}
108
72
```
109
73
74
+
You may also define custom dynamic fields by setting `options.fields``"data_field_augment"` to `true` and passing a single object argument to the logger. Object keys will be mapped to field names.
Note: If `"data_field_augment"` is set to `true` and a single object argument is NOT passed to the logger, the log message/data will still be sent as a regular field if `"data_field_name"` is also set.
79
+
110
80
## Pending work
111
81
No automated tests currently
112
82
@@ -120,4 +90,4 @@ Contributions are welcome via Pull Requests. Please submit your very first Pull
0 commit comments