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 |
|
34 | test('writes pid and host to backends', function (assert) {
|
35 | var loc = path.join(os.tmpDir(), uuid());
|
36 | var pid = process.pid;
|
37 | var host = os.hostname();
|
38 |
|
39 | var logger = Logger({
|
40 | meta: {
|
41 | team: 'rt',
|
42 | project: 'foobar',
|
43 | hostname: os.hostname(),
|
44 | pid: process.pid
|
45 | },
|
46 | backends: {
|
47 | disk: DiskBackend({
|
48 | folder: loc
|
49 | }),
|
50 | console: ConsoleBackend()
|
51 | }
|
52 | });
|
53 |
|
54 | assert.ok(captureStdio('_pid=' + pid, function () {
|
55 | logger.info('hello', { foo: 'bar' }, onlog);
|
56 | }));
|
57 |
|
58 | function onlog(err) {
|
59 | assert.ifError(err);
|
60 |
|
61 | assert.ok(captureStdio('_hostname=' + host, function () {
|
62 | logger.info('hello', { foo: 'bar' }, onlog2);
|
63 | }));
|
64 | }
|
65 |
|
66 | function onlog2(err) {
|
67 | assert.ifError(err);
|
68 |
|
69 | var fileUri = path.join(loc, 'rt-foobar.log-' +
|
70 | dateFormat('yyyyMMdd'));
|
71 |
|
72 | fs.readFile(fileUri, function (err, buf) {
|
73 | assert.ifError(err);
|
74 |
|
75 | buf = String(buf);
|
76 |
|
77 | assert.ok(buf.indexOf('hello') !== -1);
|
78 | assert.ok(buf.indexOf('foo=bar') !== -1);
|
79 | assert.ok(buf.indexOf('_pid=' + pid) !== -1);
|
80 | assert.ok(buf.indexOf('_hostname=' + host) !== -1);
|
81 |
|
82 | rimraf(loc, assert.end);
|
83 | });
|
84 | }
|
85 | });
|