|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +const stripAnsi = require('strip-ansi'); |
| 9 | + |
| 10 | +const NAMESPACE = 'mozlog_pretty' |
| 11 | + |
| 12 | +const mozlog = require('../'); |
| 13 | + |
| 14 | +var lastLog; |
| 15 | +var writer = { |
| 16 | + write: function(str) { |
| 17 | + lastLog = stripAnsi(str); |
| 18 | + return true; |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +const fxaLog = mozlog({ |
| 23 | + app: NAMESPACE, |
| 24 | + fmt: 'pretty', |
| 25 | + stream: writer, |
| 26 | +}); |
| 27 | + |
| 28 | +var logger = fxaLog('test.pretty'); |
| 29 | + |
| 30 | +/*global describe,it,beforeEach,afterEach*/ |
| 31 | + |
| 32 | +describe('pretty formatter', function() { |
| 33 | + it('should serialize objects to JSON strings', function() { |
| 34 | + var obj = { foo: { bar: 'baz' }, quux: 'baz' }; |
| 35 | + logger.info('json', obj); |
| 36 | + assert.equal(lastLog, 'mozlog_pretty.test.pretty.INFO: json {"foo":{"bar":"baz"},"quux":"baz"}\n'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should serialize circular objects', function() { |
| 40 | + var obj = { foo: {} }; |
| 41 | + obj.foo.bar = obj; |
| 42 | + logger.info('jsoncircles', obj); |
| 43 | + assert.equal(lastLog, 'mozlog_pretty.test.pretty.INFO: jsoncircles {"foo":{"bar":"[Circular]"}}\n'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should serialize errors', function() { |
| 47 | + var err1 = new Error('foo'); |
| 48 | + logger.error('errors', err1); |
| 49 | + assert(lastLog.startsWith('mozlog_pretty.test.pretty.ERROR: errors Error: foo')); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should serialize nested errors', function() { |
| 53 | + var err1 = new Error('foo'); |
| 54 | + logger.error('errors', { obj: err1 }); |
| 55 | + assert.equal(lastLog, 'mozlog_pretty.test.pretty.ERROR: errors {"obj":"Error: foo"}\n'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should coerce messages to strings', function() { |
| 59 | + var out = logger.warn('message', 42); |
| 60 | + assert.equal(lastLog, 'mozlog_pretty.test.pretty.WARN: message 42\n'); |
| 61 | + }); |
| 62 | +}); |
0 commit comments