@@ -2,6 +2,7 @@ var expect = require('chai').expect;
22const importFresh = require ( 'import-fresh' ) ;
33
44var log ;
5+ var childLogger ;
56var lastOutput ;
67
78describe ( 'Test custom fields' , function ( ) {
@@ -16,33 +17,113 @@ describe('Test custom fields', function () {
1617 } ) ;
1718 } ) ;
1819
19-
20- describe ( 'Writes log with a global custom field ' , function ( ) {
20+ describe ( 'Test writing a log with a global custom field' , function ( ) {
2121 beforeEach ( function ( ) {
22- log . setCustomFields ( { " field-a" : " value" } ) ;
23- log . logMessage ( " info" , " test-message" ) ;
22+ log . setCustomFields ( { ' field-a' : ' value' } ) ;
23+ log . logMessage ( ' info' , ' test-message' ) ;
2424 } ) ;
2525
2626 it ( 'writes a log with a custom field' , function ( ) {
2727 expect ( lastOutput ) . to . have . property ( 'field-a' , 'value' ) ;
2828 } ) ;
29+
30+ it ( 'returns the global custom field' , function ( ) {
31+ expect ( [ ...log . getCustomFields ( ) . keys ( ) ] ) . to . have . members ( [ 'field-a' ] ) ;
32+ expect ( log . getCustomFields ( ) . get ( 'field-a' ) ) . to . equal ( 'value' ) ;
33+ } ) ;
2934 } ) ;
3035
31- describe ( 'Write log with a child custom field ' , function ( ) {
36+ describe ( 'Test writing custom fields with a child logger ' , function ( ) {
3237 beforeEach ( function ( ) {
33- log . setCustomFields ( { "field-a" : "value" } ) ;
34- log . logMessage ( "info" , "test-message" ) ;
38+ log . setCustomFields ( { 'field-a' : 'value-a' } ) ;
39+ childLogger = log . createLogger ( { 'field-b' : 'value-b' } ) ;
40+ childLogger . logMessage ( 'info' , 'test-message' , { 'field-c' : 'value-c' } ) ;
3541 } ) ;
3642
37- it ( 'writes a log with a custom field' , function ( ) {
38- expect ( lastOutput ) . to . have . property ( 'field-a' , 'value' ) ;
43+ it ( 'writes a log with a global custom field' , function ( ) {
44+ expect ( lastOutput ) . to . have . property ( 'field-a' , 'value-a' ) ;
45+ } ) ;
46+
47+ it ( 'writes a log with a child logger custom field' , function ( ) {
48+ expect ( lastOutput ) . to . have . property ( 'field-b' , 'value-b' ) ;
49+ } ) ;
50+
51+ it ( 'writes a log with a custom field from logMessage call' , function ( ) {
52+ expect ( lastOutput ) . to . have . property ( 'field-c' , 'value-c' ) ;
53+ } ) ;
54+
55+ it ( 'returns the global custom field' , function ( ) {
56+ expect ( [ ...log . getCustomFields ( ) . keys ( ) ] ) . to . have . members ( [ 'field-a' ] )
57+ expect ( log . getCustomFields ( ) . get ( 'field-a' ) ) . to . equal ( 'value-a' ) ;
58+ } ) ;
59+
60+ it ( 'returns the global and the child logger custom fields' , function ( ) {
61+ expect ( [ ...childLogger . getCustomFields ( ) . keys ( ) ] ) . to . have . members ( [ 'field-a' , 'field-b' ] ) ;
62+ expect ( childLogger . getCustomFields ( ) . get ( 'field-a' ) ) . to . equal ( 'value-a' ) ;
63+ expect ( childLogger . getCustomFields ( ) . get ( 'field-b' ) ) . to . equal ( 'value-b' ) ;
3964 } ) ;
4065 } ) ;
4166
42- describe ( 'Test custom field format' , function ( ) {
67+ describe ( 'Test overriding custom fields with a child logger' , function ( ) {
68+ beforeEach ( function ( ) {
69+ log . setCustomFields ( { 'field-a' : 'value-a' } ) ;
70+ childLogger = log . createLogger ( { 'field-a' : 'value-override-a' , 'field-b' : 'value-b' } ) ;
71+ childLogger . logMessage ( 'info' , 'test-message' , { 'field-b' : 'value-override-b' } ) ;
72+ } ) ;
4373
44- describe ( '"cloud-logging" format' , function ( ) {
74+ it ( 'writes a log with an overridden global custom field' , function ( ) {
75+ expect ( lastOutput ) . to . have . property ( 'field-a' , 'value-override-a' ) ;
76+ } ) ;
4577
78+ it ( 'writes a log with an overridden child logger custom field' , function ( ) {
79+ expect ( lastOutput ) . to . have . property ( 'field-b' , 'value-override-b' ) ;
80+ } ) ;
81+
82+ it ( 'returns the original global custom field' , function ( ) {
83+ expect ( [ ...log . getCustomFields ( ) . keys ( ) ] ) . to . have . members ( [ 'field-a' ] ) ;
84+ expect ( log . getCustomFields ( ) . get ( 'field-a' ) ) . to . equal ( 'value-a' ) ;
85+ } ) ;
86+
87+ it ( 'returns the overridden global and the child logger custom fields' , function ( ) {
88+ expect ( [ ...childLogger . getCustomFields ( ) . keys ( ) ] ) . to . have . members ( [ 'field-a' , 'field-b' ] ) ;
89+ expect ( childLogger . getCustomFields ( ) . get ( 'field-a' ) ) . to . equal ( 'value-override-a' ) ;
90+ expect ( childLogger . getCustomFields ( ) . get ( 'field-b' ) ) . to . equal ( 'value-b' ) ;
91+ } ) ;
92+ } ) ;
93+
94+ describe ( 'Test writing custom fields using JS Maps' , function ( ) {
95+ beforeEach ( function ( ) {
96+ log . setCustomFields ( new Map ( ) . set ( 'field-a' , 'value-a' ) ) ;
97+ childLogger = log . createLogger ( new Map ( ) . set ( 'field-b' , 'value-b' ) ) ;
98+ childLogger . logMessage ( 'info' , 'test-message' , new Map ( ) . set ( 'field-c' , 'value-c' ) ) ;
99+ } ) ;
100+
101+ it ( 'writes a log with a global custom field' , function ( ) {
102+ expect ( lastOutput ) . to . have . property ( 'field-a' , 'value-a' ) ;
103+ } ) ;
104+
105+ it ( 'writes a log with a child logger custom field' , function ( ) {
106+ expect ( lastOutput ) . to . have . property ( 'field-b' , 'value-b' ) ;
107+ } ) ;
108+
109+ it ( 'writes a log with a custom field from logMessage call' , function ( ) {
110+ expect ( lastOutput ) . to . have . property ( 'field-c' , 'value-c' ) ;
111+ } ) ;
112+
113+ it ( 'returns the global custom field' , function ( ) {
114+ expect ( [ ...log . getCustomFields ( ) . keys ( ) ] ) . to . have . members ( [ 'field-a' ] ) ;
115+ expect ( log . getCustomFields ( ) . get ( 'field-a' ) ) . to . equal ( 'value-a' ) ;
116+ } ) ;
117+
118+ it ( 'returns the global and the child logger custom fields' , function ( ) {
119+ expect ( [ ...childLogger . getCustomFields ( ) . keys ( ) ] ) . to . have . members ( [ 'field-a' , 'field-b' ] ) ;
120+ expect ( childLogger . getCustomFields ( ) . get ( 'field-a' ) ) . to . equal ( 'value-a' ) ;
121+ expect ( childLogger . getCustomFields ( ) . get ( 'field-b' ) ) . to . equal ( 'value-b' ) ;
122+ } ) ;
123+ } ) ;
124+
125+ describe ( 'Test custom field format' , function ( ) {
126+ describe ( '"cloud-logging" format' , function ( ) {
46127 beforeEach ( function ( ) {
47128 var obj = {
48129 "cloud-logs" : { }
0 commit comments