Skip to content

Bug: critical() does not flush the log buffer when flushOnErrorLog is enabled #5210

@svozza

Description

@svozza

Expected Behavior

When flushOnErrorLog is enabled in the log buffer options, calling logger.critical() should flush the buffer before emitting the log — the same behavior as logger.error(). CRITICAL is more severe than ERROR, so it should at minimum have the same flush-on-error semantics.

Current Behavior

error() checks flushOnErrorLog and flushes the buffer (Logger.ts:411-413), but critical() (Logger.ts:387-392) does not. This means buffered diagnostic logs (debug, info, etc.) are flushed when an ERROR is logged but silently lost when a CRITICAL is logged.

Code snippet

import { Logger } from '@aws-lambda-powertools/logger';

const logger = new Logger({
  logLevel: 'ERROR',
  logBufferOptions: { enabled: true, flushOnErrorLog: true },
});

logger.debug('diagnostic info that should be preserved');
logger.critical('system failure');
// Expected: the debug log is flushed to output before the critical log
// Actual: the debug log is silently discarded

Steps to Reproduce

  1. Create a Logger with logBufferOptions: { enabled: true, flushOnErrorLog: true }
  2. Log a debug message: logger.debug('diagnostic info')
  3. Log a critical message: logger.critical('system failure')
  4. Observe that the buffered debug log was never output

Possible Solution

The most direct fix is to add the same flush check to critical(). However, a more robust approach would be to centralise the flush-on-error logic inside processLogItem(), which all log level methods already funnel through. processLogItem could check whether the incoming log level meets a severity threshold (e.g. >= LogLevelThreshold.ERROR) and flush before processing. This would make the individual methods simple one-liners and prevent the issue from recurring if new severity levels are added in the future.

Powertools for AWS Lambda (TypeScript) version

2.33.0

AWS Lambda function runtime

22.x

Packaging format used

npm


Disclaimer: After creating an issue, please wait until it is triaged and confirmed by a maintainer before implementing it. This will reduce amount of rework and the chance that a pull request gets rejected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingconfirmedThe scope is clear, ready for implementation

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions