@@ -8,7 +8,7 @@ import { ContextExamples as dummyContext, Events as dummyEvent, LambdaInterface
8
8
import { createLogger , Logger } from '../../src' ;
9
9
import { EnvironmentVariablesService } from '../../src/config' ;
10
10
import { PowertoolLogFormatter } from '../../src/formatter' ;
11
- import { ClassThatLogs , LogJsonIndent } from '../../src/types' ;
11
+ import { ClassThatLogs , LogJsonIndent , ConstructorOptions } from '../../src/types' ;
12
12
import { Context } from 'aws-lambda' ;
13
13
import { Console } from 'console' ;
14
14
@@ -1297,7 +1297,7 @@ describe('Class: Logger', () => {
1297
1297
1298
1298
describe ( 'Method: createChild' , ( ) => {
1299
1299
1300
- test ( 'Child and grandchild loggers should have all ancestor\'s options' , ( ) => {
1300
+ test ( 'child and grandchild loggers should have all ancestor\'s options' , ( ) => {
1301
1301
// Prepare
1302
1302
const INDENTATION = LogJsonIndent . COMPACT ;
1303
1303
const loggerOptions = {
@@ -1398,7 +1398,7 @@ describe('Class: Logger', () => {
1398
1398
1399
1399
} ) ;
1400
1400
1401
- test ( 'when called, it returns a DISTINCT clone of the logger instance' , ( ) => {
1401
+ test ( 'child logger should be a DISTINCT clone of the logger instance' , ( ) => {
1402
1402
1403
1403
// Prepare
1404
1404
const INDENTATION = LogJsonIndent . COMPACT ;
@@ -1716,6 +1716,94 @@ describe('Class: Logger', () => {
1716
1716
} ,
1717
1717
} ) ;
1718
1718
} ) ;
1719
+
1720
+ test ( 'child logger should have parent\'s logFormatter' , ( ) => {
1721
+
1722
+ // Prepare
1723
+ class MyCustomLogFormatter extends PowertoolLogFormatter { }
1724
+ const parentLogger = new Logger ( {
1725
+ logFormatter : new MyCustomLogFormatter ( )
1726
+ } ) ;
1727
+
1728
+ // Act
1729
+ const childLoggerWithParentLogFormatter = parentLogger . createChild ( ) ;
1730
+
1731
+ // Assess
1732
+ expect ( childLoggerWithParentLogFormatter ) . toEqual (
1733
+ expect . objectContaining ( {
1734
+ logFormatter : expect . any ( MyCustomLogFormatter ) ,
1735
+ } )
1736
+ ) ;
1737
+ } ) ;
1738
+
1739
+ test ( 'child logger with custom logFormatter in options should have provided logFormatter' , ( ) => {
1740
+
1741
+ // Prepare
1742
+ class MyCustomLogFormatter extends PowertoolLogFormatter { }
1743
+ const parentLogger = new Logger ( ) ;
1744
+
1745
+ // Act
1746
+ const childLoggerWithCustomLogFormatter = parentLogger . createChild ( {
1747
+ logFormatter : new MyCustomLogFormatter ( )
1748
+ } ) ;
1749
+
1750
+ // Assess
1751
+ expect ( parentLogger ) . toEqual (
1752
+ expect . objectContaining ( {
1753
+ logFormatter : expect . any ( PowertoolLogFormatter ) ,
1754
+ } )
1755
+ ) ;
1756
+
1757
+ expect ( childLoggerWithCustomLogFormatter ) . toEqual (
1758
+ expect . objectContaining ( {
1759
+ logFormatter : expect . any ( MyCustomLogFormatter ) ,
1760
+ } )
1761
+ ) ;
1762
+ } ) ;
1763
+
1764
+ test ( 'child logger should have exact same attributes as the parent logger created with all non-default options' , ( ) => {
1765
+
1766
+ // Prepare
1767
+ class MyCustomLogFormatter extends PowertoolLogFormatter { }
1768
+ class MyCustomEnvironmentVariablesService extends EnvironmentVariablesService { }
1769
+
1770
+ const options : ConstructorOptions = {
1771
+ logLevel : 'ERROR' ,
1772
+ serviceName : 'test-service-name' ,
1773
+ sampleRateValue : 0.77 ,
1774
+ logFormatter : new MyCustomLogFormatter ( ) ,
1775
+ customConfigService : new MyCustomEnvironmentVariablesService ( ) ,
1776
+ persistentLogAttributes : {
1777
+ aws_account_id : '1234567890' ,
1778
+ aws_region : 'eu-west-1' ,
1779
+ } ,
1780
+ environment : 'local'
1781
+ } ;
1782
+ const parentLogger = new Logger ( options ) ;
1783
+
1784
+ // Act
1785
+ const childLogger = parentLogger . createChild ( ) ;
1786
+
1787
+ // Assess
1788
+ expect ( childLogger ) . toEqual ( {
1789
+ ...parentLogger ,
1790
+ console : expect . any ( Console ) ,
1791
+ logsSampled : expect . any ( Boolean ) ,
1792
+ } ) ;
1793
+
1794
+ expect ( childLogger ) . toEqual (
1795
+ expect . objectContaining ( {
1796
+ logFormatter : expect . any ( MyCustomLogFormatter ) ,
1797
+ } )
1798
+ ) ;
1799
+
1800
+ expect ( childLogger ) . toEqual (
1801
+ expect . objectContaining ( {
1802
+ customConfigService : expect . any ( MyCustomEnvironmentVariablesService ) ,
1803
+ } )
1804
+ ) ;
1805
+
1806
+ } ) ;
1719
1807
1720
1808
} ) ;
1721
1809
0 commit comments