Skip to content

Commit

Permalink
feat(logger): introduce loglevel constant (#2787)
Browse files Browse the repository at this point in the history
  • Loading branch information
marlapativ authored Jul 20, 2024
1 parent 3cedd9f commit e75f593
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 31 deletions.
11 changes: 10 additions & 1 deletion packages/logger/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ const LogJsonIndent = {
COMPACT: 0,
} as const;

export { LogJsonIndent };
const LogLevel = {
DEBUG: 'DEBUG',
INFO: 'INFO',
WARN: 'WARN',
ERROR: 'ERROR',
SILENT: 'SILENT',
CRITICAL: 'CRITICAL',
} as const;

export { LogJsonIndent, LogLevel };
1 change: 1 addition & 0 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Logger } from './Logger.js';
export { LogFormatter } from './formatter/LogFormatter.js';
export { LogItem } from './formatter/LogItem.js';
export { LogLevel } from './constants.js';
22 changes: 3 additions & 19 deletions packages/logger/src/types/Log.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
import type { LogItem } from '../formatter/LogItem.js';
import type { UnformattedAttributes } from './Logger.js';

type LogLevelDebug = 'DEBUG';
type LogLevelInfo = 'INFO';
type LogLevelWarn = 'WARN';
type LogLevelError = 'ERROR';
type LogLevelSilent = 'SILENT';
type LogLevelCritical = 'CRITICAL';
import { LogLevel } from '../constants.js';

type LogLevel =
| LogLevelDebug
| Lowercase<LogLevelDebug>
| LogLevelInfo
| Lowercase<LogLevelInfo>
| LogLevelWarn
| Lowercase<LogLevelWarn>
| LogLevelError
| Lowercase<LogLevelError>
| LogLevelSilent
| Lowercase<LogLevelSilent>
| LogLevelCritical
| Lowercase<LogLevelCritical>;
| (typeof LogLevel)[keyof typeof LogLevel]
| Lowercase<(typeof LogLevel)[keyof typeof LogLevel]>;

type LogLevelThresholds = {
[key in Uppercase<LogLevel>]: number;
Expand Down
25 changes: 14 additions & 11 deletions packages/logger/tests/unit/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
*/
import context from '@aws-lambda-powertools/testing-utils/context';
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
import { Logger, LogFormatter } from '../../src/index.js';
import { Logger, LogFormatter, LogLevel } from '../../src/index.js';
import { ConfigServiceInterface } from '../../src/types/ConfigServiceInterface.js';
import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js';
import { PowertoolsLogFormatter } from '../../src/formatter/PowertoolsLogFormatter.js';
import { LogLevelThresholds, LogLevel } from '../../src/types/Log.js';
import {
LogLevelThresholds,
type LogLevel as LogLevelType,
} from '../../src/types/Log.js';
import {
type LogFunction,
type ConstructorOptions,
Expand Down Expand Up @@ -598,7 +601,7 @@ describe('Class: Logger', () => {
test(`when the level is DEBUG, it ${debugAction} print to stdout`, () => {
// Prepare
const logger = new Logger({
logLevel: 'DEBUG',
logLevel: LogLevel.DEBUG,
});
const consoleSpy = jest.spyOn(
logger['console'],
Expand Down Expand Up @@ -656,7 +659,7 @@ describe('Class: Logger', () => {
test(`when the log level is WARN, it ${warnAction} print to stdout`, () => {
// Prepare
const logger = new Logger({
logLevel: 'WARN',
logLevel: LogLevel.WARN,
});
const consoleSpy = jest.spyOn(
logger['console'],
Expand Down Expand Up @@ -714,7 +717,7 @@ describe('Class: Logger', () => {
test('when the log level is SILENT, it DOES NOT print to stdout', () => {
// Prepare
const logger = new Logger({
logLevel: 'SILENT',
logLevel: LogLevel.SILENT,
});
const consoleSpy = jest.spyOn(
logger['console'],
Expand Down Expand Up @@ -2347,7 +2350,7 @@ describe('Class: Logger', () => {
test('when logEvent is enabled, it logs the event in the first log', async () => {
// Prepare
const logger = new Logger({
logLevel: 'DEBUG',
logLevel: LogLevel.DEBUG,
});
const consoleSpy = jest.spyOn(logger['console'], 'info');
class LambdaFunction implements LambdaInterface {
Expand Down Expand Up @@ -3141,7 +3144,7 @@ describe('Class: Logger', () => {
const logger = new Logger();

// Act
logger.setLogLevel('ERROR');
logger.setLogLevel(LogLevel.ERROR);

// Assess
expect(logger.level).toBe(20);
Expand All @@ -3153,7 +3156,7 @@ describe('Class: Logger', () => {
const logger = new Logger();

// Act & Assess
expect(() => logger.setLogLevel('INVALID' as LogLevel)).toThrow(
expect(() => logger.setLogLevel('INVALID' as LogLevelType)).toThrow(
'Invalid log level: INVALID'
);
});
Expand Down Expand Up @@ -3240,7 +3243,7 @@ describe('Class: Logger', () => {
process.env.POWERTOOLS_LOGGER_SAMPLE_RATE = '1';

const logger: Logger = new Logger({
logLevel: 'ERROR',
logLevel: LogLevel.ERROR,
});

// Assess
Expand Down Expand Up @@ -3396,7 +3399,7 @@ describe('Class: Logger', () => {
test('when sample rate in constructor is out of expected range, it should be ignored', () => {
// Prepare
const logger: Logger = new Logger({
logLevel: 'INFO',
logLevel: LogLevel.INFO,
sampleRateValue: 42,
});
const consoleSpy = jest.spyOn(logger['console'], 'info');
Expand Down Expand Up @@ -3498,7 +3501,7 @@ describe('Class: Logger', () => {
test('when sample rate calculation is refreshed, it DOES NOT overwrite the sample rate value', () => {
// Prepare
const logger = new Logger({
logLevel: 'INFO',
logLevel: LogLevel.INFO,
sampleRateValue: 1,
});
const consoleSpy = jest.spyOn(logger['console'], 'info');
Expand Down

0 comments on commit e75f593

Please sign in to comment.