@@ -1476,15 +1476,9 @@ describe('Class: Logger', () => {
1476
1476
1477
1477
// Assess
1478
1478
expect ( consoleSpy ) . toHaveBeenCalledTimes ( 1 ) ;
1479
- expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1480
- 1 ,
1481
- JSON . stringify ( {
1482
- level : 'INFO' ,
1483
- message : 'This is an INFO log with some log attributes' ,
1484
- sampling_rate : 0 ,
1485
- service : 'hello-world' ,
1486
- timestamp : '2016-06-20T12:08:10.000Z' ,
1487
- xray_trace_id : '1-5759e988-bd862e3fe1be46a994272793' ,
1479
+ const log = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
1480
+ expect ( log ) . toStrictEqual (
1481
+ expect . objectContaining ( {
1488
1482
aws_account_id : '0987654321' ,
1489
1483
} )
1490
1484
) ;
@@ -1758,6 +1752,52 @@ describe('Class: Logger', () => {
1758
1752
} ) ;
1759
1753
} ) ;
1760
1754
1755
+ describe ( 'Method: appendPersistentKeys' , ( ) => {
1756
+ it ( 'overwrites existing persistent keys with new ones' , ( ) => {
1757
+ // Prepare
1758
+ const logger = new Logger ( {
1759
+ persistentKeys : {
1760
+ aws_account_id : '123456789012' ,
1761
+ } ,
1762
+ } ) ;
1763
+
1764
+ // Act
1765
+ logger . appendPersistentKeys ( {
1766
+ aws_account_id : '0987654321' ,
1767
+ } ) ;
1768
+
1769
+ // Assess
1770
+ expect ( logger ) . toEqual (
1771
+ expect . objectContaining ( {
1772
+ persistentLogAttributes : {
1773
+ aws_account_id : '0987654321' ,
1774
+ } ,
1775
+ } )
1776
+ ) ;
1777
+ } ) ;
1778
+
1779
+ it ( 'overwrites existing temporary keys with new ones in the next log' , ( ) => {
1780
+ // Prepare
1781
+ const logger = new Logger ( ) ;
1782
+ const debugSpy = jest . spyOn ( logger [ 'console' ] , 'info' ) ;
1783
+ logger . appendKeys ( {
1784
+ aws_account_id : '123456789012' ,
1785
+ } ) ;
1786
+
1787
+ // Act
1788
+ logger . appendPersistentKeys ( {
1789
+ aws_account_id : '0987654321' ,
1790
+ } ) ;
1791
+ logger . info ( 'This is an INFO log with some log attributes' ) ;
1792
+
1793
+ // Assess
1794
+ const log = JSON . parse ( debugSpy . mock . calls [ 0 ] [ 0 ] ) ;
1795
+ expect ( log ) . toStrictEqual (
1796
+ expect . objectContaining ( { aws_account_id : '0987654321' } )
1797
+ ) ;
1798
+ } ) ;
1799
+ } ) ;
1800
+
1761
1801
describe ( 'Method: injectLambdaContext' , ( ) => {
1762
1802
beforeEach ( ( ) => {
1763
1803
jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => ( { } ) ) ;
@@ -1788,10 +1828,10 @@ describe('Class: Logger', () => {
1788
1828
await handler ( event , context ) ;
1789
1829
1790
1830
// Assess
1791
- expect ( consoleSpy ) . toBeCalledTimes ( 1 ) ;
1792
- expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1793
- 1 ,
1794
- JSON . stringify ( {
1831
+ expect ( consoleSpy ) . toHaveBeenCalledTimes ( 1 ) ;
1832
+ const log = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
1833
+ expect ( log ) . toEqual (
1834
+ expect . objectContaining ( {
1795
1835
cold_start : true ,
1796
1836
function_arn :
1797
1837
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
@@ -1830,10 +1870,10 @@ describe('Class: Logger', () => {
1830
1870
1831
1871
// Assess
1832
1872
1833
- expect ( consoleSpy ) . toBeCalledTimes ( 2 ) ;
1834
- expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1835
- 1 ,
1836
- JSON . stringify ( {
1873
+ expect ( consoleSpy ) . toHaveBeenCalledTimes ( 2 ) ;
1874
+ const log1 = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
1875
+ expect ( log1 ) . toStrictEqual (
1876
+ expect . objectContaining ( {
1837
1877
level : 'INFO' ,
1838
1878
message : 'An INFO log without context!' ,
1839
1879
sampling_rate : 0 ,
@@ -1842,9 +1882,9 @@ describe('Class: Logger', () => {
1842
1882
xray_trace_id : '1-5759e988-bd862e3fe1be46a994272793' ,
1843
1883
} )
1844
1884
) ;
1845
- expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1846
- 2 ,
1847
- JSON . stringify ( {
1885
+ const log2 = JSON . parse ( consoleSpy . mock . calls [ 1 ] [ 0 ] ) ;
1886
+ expect ( log2 ) . toStrictEqual (
1887
+ expect . objectContaining ( {
1848
1888
cold_start : true ,
1849
1889
function_arn :
1850
1890
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
@@ -1887,10 +1927,10 @@ describe('Class: Logger', () => {
1887
1927
// Assess
1888
1928
1889
1929
expect ( actualResult ) . toEqual ( expectedReturnValue ) ;
1890
- expect ( consoleSpy ) . toBeCalledTimes ( 2 ) ;
1891
- expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1892
- 1 ,
1893
- JSON . stringify ( {
1930
+ expect ( consoleSpy ) . toHaveBeenCalledTimes ( 2 ) ;
1931
+ const log1 = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
1932
+ expect ( log1 ) . toStrictEqual (
1933
+ expect . objectContaining ( {
1894
1934
level : 'INFO' ,
1895
1935
message : 'An INFO log without context!' ,
1896
1936
sampling_rate : 0 ,
@@ -1899,9 +1939,9 @@ describe('Class: Logger', () => {
1899
1939
xray_trace_id : '1-5759e988-bd862e3fe1be46a994272793' ,
1900
1940
} )
1901
1941
) ;
1902
- expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1903
- 2 ,
1904
- JSON . stringify ( {
1942
+ const log2 = JSON . parse ( consoleSpy . mock . calls [ 1 ] [ 0 ] ) ;
1943
+ expect ( log2 ) . toStrictEqual (
1944
+ expect . objectContaining ( {
1905
1945
cold_start : true ,
1906
1946
function_arn :
1907
1947
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
@@ -1953,8 +1993,9 @@ describe('Class: Logger', () => {
1953
1993
1954
1994
// Assess
1955
1995
expect ( debugSpy ) . toHaveBeenCalledTimes ( 2 ) ;
1956
- expect ( debugSpy ) . toHaveBeenCalledWith (
1957
- JSON . stringify ( {
1996
+ const log1 = JSON . parse ( debugSpy . mock . calls [ 0 ] [ 0 ] ) ;
1997
+ expect ( log1 ) . toStrictEqual (
1998
+ expect . objectContaining ( {
1958
1999
cold_start : true ,
1959
2000
function_arn :
1960
2001
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
@@ -1972,8 +2013,9 @@ describe('Class: Logger', () => {
1972
2013
details : { user_id : '1234' } ,
1973
2014
} )
1974
2015
) ;
1975
- expect ( debugSpy ) . toHaveBeenLastCalledWith (
1976
- JSON . stringify ( {
2016
+ const log2 = JSON . parse ( debugSpy . mock . calls [ 1 ] [ 0 ] ) ;
2017
+ expect ( log2 ) . toStrictEqual (
2018
+ expect . objectContaining ( {
1977
2019
cold_start : true ,
1978
2020
function_arn :
1979
2021
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
@@ -1988,8 +2030,6 @@ describe('Class: Logger', () => {
1988
2030
xray_trace_id : '1-5759e988-bd862e3fe1be46a994272793' ,
1989
2031
} )
1990
2032
) ;
1991
-
1992
- debugSpy . mockRestore ( ) ;
1993
2033
} ) ;
1994
2034
1995
2035
test ( 'when clearState is enabled, the persistent log attributes added in the handler ARE NOT cleared when the method returns' , async ( ) => {
@@ -2008,7 +2048,7 @@ describe('Class: Logger', () => {
2008
2048
_context : Context
2009
2049
) : Promise < void > {
2010
2050
// These persistent attributes stay persistent
2011
- logger . addPersistentLogAttributes ( {
2051
+ logger . appendPersistentKeys ( {
2012
2052
details : { user_id : '1234' } ,
2013
2053
} ) ;
2014
2054
logger . debug ( 'This is a DEBUG log with the user_id' ) ;
@@ -2054,7 +2094,7 @@ describe('Class: Logger', () => {
2054
2094
_context : Context
2055
2095
) : Promise < void > {
2056
2096
// This key is persistent and will stay persistent
2057
- logger . addPersistentLogAttributes ( {
2097
+ logger . appendPersistentKeys ( {
2058
2098
foo : 'bar' ,
2059
2099
} ) ;
2060
2100
// This attribute is temporary and will be cleared
@@ -2075,8 +2115,9 @@ describe('Class: Logger', () => {
2075
2115
2076
2116
// Assess
2077
2117
expect ( debugSpy ) . toHaveBeenCalledTimes ( 2 ) ;
2078
- expect ( debugSpy ) . toHaveBeenCalledWith (
2079
- JSON . stringify ( {
2118
+ const log1 = JSON . parse ( debugSpy . mock . calls [ 0 ] [ 0 ] ) ;
2119
+ expect ( log1 ) . toStrictEqual (
2120
+ expect . objectContaining ( {
2080
2121
cold_start : true ,
2081
2122
function_arn :
2082
2123
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
@@ -2093,8 +2134,9 @@ describe('Class: Logger', () => {
2093
2134
biz : 'baz' ,
2094
2135
} )
2095
2136
) ;
2096
- expect ( debugSpy ) . toHaveBeenLastCalledWith (
2097
- JSON . stringify ( {
2137
+ const log2 = JSON . parse ( debugSpy . mock . calls [ 1 ] [ 0 ] ) ;
2138
+ expect ( log2 ) . toStrictEqual (
2139
+ expect . objectContaining ( {
2098
2140
cold_start : true ,
2099
2141
function_arn :
2100
2142
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
@@ -2129,7 +2171,7 @@ describe('Class: Logger', () => {
2129
2171
_context : Context
2130
2172
) : Promise < string > {
2131
2173
// This key is persistent and will stay persistent
2132
- logger . addPersistentLogAttributes ( {
2174
+ logger . appendPersistentKeys ( {
2133
2175
foo : 'bar' ,
2134
2176
} ) ;
2135
2177
// This attribute is temporary and will be cleared
@@ -2149,8 +2191,9 @@ describe('Class: Logger', () => {
2149
2191
await expect ( handler ( event , context ) ) . rejects . toThrow ( ) ;
2150
2192
2151
2193
expect ( debugSpy ) . toHaveBeenCalledTimes ( 1 ) ;
2152
- expect ( debugSpy ) . toHaveBeenCalledWith (
2153
- JSON . stringify ( {
2194
+ const log = JSON . parse ( debugSpy . mock . calls [ 0 ] [ 0 ] ) ;
2195
+ expect ( log ) . toStrictEqual (
2196
+ expect . objectContaining ( {
2154
2197
cold_start : true ,
2155
2198
function_arn :
2156
2199
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
@@ -2202,10 +2245,10 @@ describe('Class: Logger', () => {
2202
2245
await handler ( event , context ) ;
2203
2246
2204
2247
// Assess
2205
- expect ( consoleSpy ) . toBeCalledTimes ( 1 ) ;
2206
- expect ( consoleSpy ) . toHaveBeenNthCalledWith (
2207
- 1 ,
2208
- JSON . stringify ( {
2248
+ expect ( consoleSpy ) . toHaveBeenCalledTimes ( 1 ) ;
2249
+ const log = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
2250
+ expect ( log ) . toStrictEqual (
2251
+ expect . objectContaining ( {
2209
2252
cold_start : true ,
2210
2253
function_arn :
2211
2254
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
@@ -2249,10 +2292,10 @@ describe('Class: Logger', () => {
2249
2292
await handler ( event , context ) ;
2250
2293
2251
2294
// Assess
2252
- expect ( consoleSpy ) . toBeCalledTimes ( 1 ) ;
2253
- expect ( consoleSpy ) . toHaveBeenNthCalledWith (
2254
- 1 ,
2255
- JSON . stringify ( {
2295
+ expect ( consoleSpy ) . toHaveBeenCalledTimes ( 1 ) ;
2296
+ const log = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
2297
+ expect ( log ) . toStrictEqual (
2298
+ expect . objectContaining ( {
2256
2299
cold_start : true ,
2257
2300
function_arn :
2258
2301
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
@@ -2307,10 +2350,10 @@ describe('Class: Logger', () => {
2307
2350
await handler ( { } , context ) ;
2308
2351
2309
2352
// Assess
2310
- expect ( consoleSpy ) . toBeCalledTimes ( 1 ) ;
2311
- expect ( consoleSpy ) . toHaveBeenNthCalledWith (
2312
- 1 ,
2313
- JSON . stringify ( {
2353
+ expect ( consoleSpy ) . toHaveBeenCalledTimes ( 1 ) ;
2354
+ const log = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
2355
+ expect ( log ) . toStrictEqual (
2356
+ expect . objectContaining ( {
2314
2357
cold_start : true ,
2315
2358
function_arn :
2316
2359
'arn:aws:lambda:eu-west-1:123456789012:function:foo-bar-function' ,
@@ -2704,7 +2747,7 @@ describe('Class: Logger', () => {
2704
2747
const childLogger = parentLogger . createChild ( ) ;
2705
2748
2706
2749
// Act
2707
- parentLogger . addPersistentLogAttributes ( {
2750
+ parentLogger . appendPersistentKeys ( {
2708
2751
aws_account_id : '123456789012' ,
2709
2752
aws_region : 'eu-west-1' ,
2710
2753
logger : {
0 commit comments