|
| 1 | +import { log } from 'node:console'; |
1 | 2 | import { setTimeout } from 'node:timers/promises'; |
2 | 3 | import context from '@aws-lambda-powertools/testing-utils/context'; |
3 | 4 | import middy from '@middy/core'; |
@@ -825,4 +826,200 @@ describe('Working with keys', () => { |
825 | 826 | ); |
826 | 827 | } |
827 | 828 | ); |
| 829 | + |
| 830 | + describe('deprecated persistentLogAttributes usage', () => { |
| 831 | + it('should set #attributesStore on the logger', () => { |
| 832 | + // Prepare |
| 833 | + const logger = new Logger({ |
| 834 | + persistentLogAttributes: { |
| 835 | + foo: 'bar', |
| 836 | + }, |
| 837 | + }); |
| 838 | + |
| 839 | + // Assess |
| 840 | + expect(logger.getPersistentLogAttributes()).toEqual({ |
| 841 | + foo: 'bar', |
| 842 | + }); |
| 843 | + |
| 844 | + expect(console.warn).not.toHaveLogged( |
| 845 | + expect.objectContaining({ |
| 846 | + message: |
| 847 | + 'Both persistentLogAttributes and persistentKeys options were provided. Using persistentKeys as persistentLogAttributes is deprecated and will be removed in future releases', |
| 848 | + }) |
| 849 | + ); |
| 850 | + }); |
| 851 | + |
| 852 | + it('should get overridden by persistentKeys usage', () => { |
| 853 | + // Prepare |
| 854 | + const logger = new Logger({ |
| 855 | + persistentKeys: { |
| 856 | + foo: 'bar', |
| 857 | + }, |
| 858 | + // @ts-expect-error |
| 859 | + persistentLogAttributes: { |
| 860 | + foo: 'not-bar', |
| 861 | + }, |
| 862 | + }); |
| 863 | + |
| 864 | + // Assess |
| 865 | + expect(logger.getPersistentLogAttributes()).toEqual({ |
| 866 | + foo: 'bar', |
| 867 | + }); |
| 868 | + |
| 869 | + expect(console.warn).toHaveLogged( |
| 870 | + expect.objectContaining({ |
| 871 | + message: |
| 872 | + 'Both persistentLogAttributes and persistentKeys options were provided. Using persistentKeys as persistentLogAttributes is deprecated and will be removed in future releases', |
| 873 | + }) |
| 874 | + ); |
| 875 | + }); |
| 876 | + |
| 877 | + it('should persist for child loggers', () => { |
| 878 | + // Prepare |
| 879 | + const logger = new Logger({ |
| 880 | + persistentLogAttributes: { |
| 881 | + foo: 'bar', |
| 882 | + }, |
| 883 | + }); |
| 884 | + |
| 885 | + // Act |
| 886 | + const child = logger.createChild(); |
| 887 | + |
| 888 | + // Assess |
| 889 | + expect(child.getPersistentLogAttributes()).toEqual({ |
| 890 | + foo: 'bar', |
| 891 | + }); |
| 892 | + |
| 893 | + expect(console.warn).not.toHaveLogged( |
| 894 | + expect.objectContaining({ |
| 895 | + message: |
| 896 | + 'Both persistentLogAttributes and persistentKeys options were provided. Using persistentKeys as persistentLogAttributes is deprecated and will be removed in future releases', |
| 897 | + }) |
| 898 | + ); |
| 899 | + }); |
| 900 | + |
| 901 | + it('should persist for child loggers using persistentLogAttributes', () => { |
| 902 | + // Prepare |
| 903 | + const logger = new Logger({ |
| 904 | + persistentLogAttributes: { |
| 905 | + foo: 'bar', |
| 906 | + }, |
| 907 | + }); |
| 908 | + |
| 909 | + // Act |
| 910 | + const child = logger.createChild({ |
| 911 | + persistentLogAttributes: { |
| 912 | + bar: 'foo', |
| 913 | + }, |
| 914 | + }); |
| 915 | + |
| 916 | + // Assess |
| 917 | + expect(child.getPersistentLogAttributes()).toEqual({ |
| 918 | + foo: 'bar', |
| 919 | + bar: 'foo', |
| 920 | + }); |
| 921 | + |
| 922 | + expect(console.warn).not.toHaveLogged( |
| 923 | + expect.objectContaining({ |
| 924 | + message: |
| 925 | + 'Both persistentLogAttributes and persistentKeys options were provided. Using persistentKeys as persistentLogAttributes is deprecated and will be removed in future releases', |
| 926 | + }) |
| 927 | + ); |
| 928 | + }); |
| 929 | + |
| 930 | + it('should persist for child loggers using persistKeys', () => { |
| 931 | + // Prepare |
| 932 | + const logger = new Logger({ |
| 933 | + persistentLogAttributes: { |
| 934 | + foo: 'bar', |
| 935 | + }, |
| 936 | + }); |
| 937 | + |
| 938 | + // Act |
| 939 | + const child = logger.createChild({ |
| 940 | + persistentKeys: { |
| 941 | + bar: 'foo', |
| 942 | + }, |
| 943 | + }); |
| 944 | + |
| 945 | + // Assess |
| 946 | + expect(child.getPersistentLogAttributes()).toEqual({ |
| 947 | + foo: 'bar', |
| 948 | + bar: 'foo', |
| 949 | + }); |
| 950 | + |
| 951 | + expect(console.warn).not.toHaveLogged( |
| 952 | + expect.objectContaining({ |
| 953 | + message: |
| 954 | + 'Both persistentLogAttributes and persistentKeys options were provided. Using persistentKeys as persistentLogAttributes is deprecated and will be removed in future releases', |
| 955 | + }) |
| 956 | + ); |
| 957 | + }); |
| 958 | + |
| 959 | + it('should persist for child loggers using persistentLogAttributes when parent used persistentLogAttributes', () => { |
| 960 | + // Prepare |
| 961 | + const logger = new Logger({ |
| 962 | + persistentLogAttributes: { |
| 963 | + foo: 'bar', |
| 964 | + }, |
| 965 | + }); |
| 966 | + |
| 967 | + // Act |
| 968 | + const child = logger.createChild({ |
| 969 | + persistentKeys: { |
| 970 | + bar: 'foo', |
| 971 | + }, |
| 972 | + // @ts-expect-error |
| 973 | + persistentLogAttributes: { |
| 974 | + bar: 'not-foo', |
| 975 | + }, |
| 976 | + }); |
| 977 | + |
| 978 | + // Assess |
| 979 | + expect(child.getPersistentLogAttributes()).toEqual({ |
| 980 | + foo: 'bar', |
| 981 | + bar: 'foo', |
| 982 | + }); |
| 983 | + |
| 984 | + expect(console.warn).toHaveLogged( |
| 985 | + expect.objectContaining({ |
| 986 | + message: |
| 987 | + 'Both persistentLogAttributes and persistentKeys options were provided. Using persistentKeys as persistentLogAttributes is deprecated and will be removed in future releases', |
| 988 | + }) |
| 989 | + ); |
| 990 | + }); |
| 991 | + |
| 992 | + it('should persist for child loggers using persistentLogAttributes when parent used persistentKeys', () => { |
| 993 | + // Prepare |
| 994 | + const logger = new Logger({ |
| 995 | + persistentKeys: { |
| 996 | + foo: 'bar', |
| 997 | + }, |
| 998 | + }); |
| 999 | + |
| 1000 | + // Act |
| 1001 | + const child = logger.createChild({ |
| 1002 | + persistentKeys: { |
| 1003 | + bar: 'foo', |
| 1004 | + }, |
| 1005 | + // @ts-expect-error |
| 1006 | + persistentLogAttributes: { |
| 1007 | + bar: 'not-foo', |
| 1008 | + }, |
| 1009 | + }); |
| 1010 | + |
| 1011 | + // Assess |
| 1012 | + expect(child.getPersistentLogAttributes()).toEqual({ |
| 1013 | + foo: 'bar', |
| 1014 | + bar: 'foo', |
| 1015 | + }); |
| 1016 | + |
| 1017 | + expect(console.warn).toHaveLogged( |
| 1018 | + expect.objectContaining({ |
| 1019 | + message: |
| 1020 | + 'Both persistentLogAttributes and persistentKeys options were provided. Using persistentKeys as persistentLogAttributes is deprecated and will be removed in future releases', |
| 1021 | + }) |
| 1022 | + ); |
| 1023 | + }); |
| 1024 | + }); |
828 | 1025 | }); |
0 commit comments