1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | var test = require('tape');
|
22 | var path = require('path');
|
23 | var os = require('os');
|
24 | var uuid = require('uuid');
|
25 | var fs = require('fs');
|
26 | var dateFormat = require('date-format');
|
27 | var rimraf = require('rimraf');
|
28 |
|
29 | var captureStdio = require('./lib/capture-stdio.js');
|
30 | var Logger = require('../logger.js');
|
31 | var DiskBackend = require('../backends/disk.js');
|
32 | var ConsoleBackend = require('../backends/console.js');
|
33 | var Entry = require('../entry.js');
|
34 |
|
35 | function AddFoo(baseMeta) {
|
36 | return addFoo;
|
37 |
|
38 | function addFoo(entry) {
|
39 | var meta = entry.meta || {};
|
40 |
|
41 | if (!meta._foo && baseMeta.foo) {
|
42 | meta._foo = baseMeta.foo;
|
43 | }
|
44 |
|
45 | return new Entry(entry.level, entry.message, entry.meta, entry.path);
|
46 | }
|
47 | }
|
48 |
|
49 | test('writes extended meta to backends', function (assert) {
|
50 | var loc = path.join(os.tmpDir(), uuid());
|
51 | var pid = process.pid;
|
52 | var foo = 'foop';
|
53 | var host = os.hostname();
|
54 |
|
55 | var logger = Logger({
|
56 | meta: {
|
57 | team: 'rt',
|
58 | project: 'foobar',
|
59 | hostname: os.hostname(),
|
60 | pid: process.pid,
|
61 | foo: foo
|
62 | },
|
63 | basemetaTransforms: [AddFoo],
|
64 | backends: {
|
65 | disk: DiskBackend({
|
66 | folder: loc
|
67 | }),
|
68 | console: ConsoleBackend()
|
69 | }
|
70 | });
|
71 |
|
72 | assert.ok(captureStdio('_foo=' + foo, function () {
|
73 | logger.info('hello', { foo: 'bar' }, onlog);
|
74 | }));
|
75 |
|
76 | function onlog(err) {
|
77 | assert.ifError(err);
|
78 |
|
79 | assert.ok(captureStdio('_hostname=' + host, function () {
|
80 | logger.meta.foo = 'bar';
|
81 | foo = 'bar';
|
82 | logger.info('hello', { foo: 'bar' }, onlog2);
|
83 | }));
|
84 | }
|
85 |
|
86 | function onlog2(err) {
|
87 | assert.ifError(err);
|
88 |
|
89 | var fileUri = path.join(loc, 'rt-foobar.log-' +
|
90 | dateFormat('yyyyMMdd'));
|
91 |
|
92 | fs.readFile(fileUri, function (err, buf) {
|
93 | assert.ifError(err);
|
94 |
|
95 | buf = String(buf);
|
96 |
|
97 | assert.ok(buf.indexOf('hello') !== -1);
|
98 | assert.ok(buf.indexOf('foo=bar') !== -1);
|
99 | assert.ok(buf.indexOf('_pid=' + pid) !== -1);
|
100 | assert.ok(buf.indexOf('_foo=' + foo) !== -1);
|
101 | assert.ok(buf.indexOf('_hostname=' + host) !== -1);
|
102 |
|
103 | rimraf(loc, assert.end);
|
104 | });
|
105 | }
|
106 | });
|