Skip to content

Add structured JSON logging support#58

Open
anzheyazzz wants to merge 2 commits intomainfrom
anzhey/structured-logging
Open

Add structured JSON logging support#58
anzheyazzz wants to merge 2 commits intomainfrom
anzhey/structured-logging

Conversation

@anzheyazzz
Copy link

Description of changes:

Add structured JSON logging support to the Ruby RIC, consistent with
the structured logging behavior in other Lambda managed runtimes (Python, Node.js, etc.).

When AWS_LAMBDA_LOG_FORMAT=JSON, each log output is a JSON object containing
at least four key-value pairs:

  • timestamp – the time the log message was generated (ISO 8601, UTC)
  • level – the log level assigned to the message (DEBUG, INFO, WARN, ERROR, FATAL)
  • message – the contents of the log message
  • requestId – the unique request ID for the function invocation

This PR includes two commits:

  1. fix: Update xray_cause_test for Ruby 3.4+ backtrace format

    • Ruby 3.4+ changed Thread::Backtrace::Location#label from method_name to ClassName#method_name. Use assert_includes to handle both formats.
  2. feat: Add structured JSON logging support

    • Add LambdaLogFormatter (JSON mode) with structured fields including exception support
    • Update LoggerPatch to select formatter and log level based on environment variables
    • Refactor TelemetryLogger initialization to apply Logger patch and runtime config before telemetry fd setup
    • Fix LogFormatter (TEXT mode) to append newline for consistency
    • Add unit tests for LambdaLogFormatter, LambdaLogger, and LoggerPatch

Target (OCI, Managed Runtime, both):

Both

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Example Usage:

require 'logger'

def lambda_handler(event:, context:)
    logger = Logger.new($stdout)
    logger.info("Inside the handler function")
end

Output without AWS_LAMBDA_LOG_FORMAT:

I, [2026-03-10T22:30:46.745494 #16] INFO 3244ba10-b213-4742-9cbc-fcc611f42625 -- : Inside the handler function

Output with AWS_LAMBDA_LOG_FORMAT=JSON:

{
	"timestamp":"2026-03-10T21:37:36.350555Z",
	"level":"INFO",
	"message":"Inside the handler function",
	"requestId":"d1491d62-2ed4-4d08-a1be-feea698100f7"
}

Example exceptions:

require 'logger'

def lambda_handler(event:, context:)
    logger = Logger.new($stdout)
    begin
        raise "something went wrong"
    rescue => e
        logger.error(e)
    end
end

Output without AWS_LAMBDA_LOG_FORMAT:

E, [2026-03-10T22:38:29.311464 #16] ERROR f2a7b2cf-ce51-4b7b-b280-a92eea3f8bf5 -- : something went wrong (RuntimeError)
/var/task/lambda_function.rb:5:in 'Object#lambda_handler'
/var/runtime/gems/aws_lambda_ric-3.1.3/lib/aws_lambda_ric/lambda_handler.rb:28:in 'LambdaHandler#call_handler'
/var/runtime/gems/aws_lambda_ric-3.1.3/lib/aws_lambda_ric.rb:86:in 'AwsLambdaRIC::LambdaRunner#run_user_code'
/var/runtime/gems/aws_lambda_ric-3.1.3/lib/aws_lambda_ric.rb:64:in 'AwsLambdaRIC::LambdaRunner#start_runtime_loop'
/var/runtime/gems/aws_lambda_ric-3.1.3/lib/aws_lambda_ric.rb:47:in 'AwsLambdaRIC::LambdaRunner#run'
/var/runtime/gems/aws_lambda_ric-3.1.3/lib/aws_lambda_ric.rb:235:in 'AwsLambdaRIC::Bootstrap#bootstrap_handler'
/var/runtime/gems/aws_lambda_ric-3.1.3/lib/aws_lambda_ric.rb:217:in 'AwsLambdaRIC::Bootstrap#start'
/var/runtime/index.rb:4:in '<main>'

Output with AWS_LAMBDA_LOG_FORMAT=JSON:

{
	"timestamp":"2026-03-10T22:51:58.675676Z",
	"level":"ERROR",
	"message":"something went wrong",
	"requestId":"f1890346-7a2f-4adf-95de-d90ba9f7d1b8",
	"errorType":"RuntimeError",
	"errorMessage":"something went wrong",
	"stackTrace":["/var/task/lambda_function.rb:6:in 'Object#lambda_handler'","/var/runtime/gems/aws_lambda_ric-3.1.3/lib/aws_lambda_ric/lambda_handler.rb:28:in 'LambdaHandler#call_handler'","/var/runtime/gems/aws_lambda_ric-3.1.3/lib/aws_lambda_ric.rb:86:in 'AwsLambdaRIC::LambdaRunner#run_user_code'","/var/runtime/gems/aws_lambda_ric-3.1.3/lib/aws_lambda_ric.rb:64:in 'AwsLambdaRIC::LambdaRunner#start_runtime_loop'","/var/runtime/gems/aws_lambda_ric-3.1.3/lib/aws_lambda_ric.rb:47:in 'AwsLambdaRIC::LambdaRunner#run'","/var/runtime/gems/aws_lambda_ric-3.1.3/lib/aws_lambda_ric.rb:235:in 'AwsLambdaRIC::Bootstrap#bootstrap_handler'","/var/runtime/gems/aws_lambda_ric-3.1.3/lib/aws_lambda_ric.rb:217:in 'AwsLambdaRIC::Bootstrap#start'","/var/runtime/index.rb:4:in '<main>'"],
	"location":"/var/task/lambda_function.rb:Object#lambda_handler:6"
}

Ruby 3.4+ changed backtrace label from 'method_name' to
'ClassName#method_name'. Use assert_includes to handle both formats.
Add JsonLogFormatter for JSON-formatted log output controlled by
AWS_LAMBDA_LOG_FORMAT and AWS_LAMBDA_LOG_LEVEL environment variables.
Refactor TelemetryLogger initialization to apply Logger patch and
runtime config before telemetry fd setup.

- Add JsonLogFormatter with timestamp, level, message, requestId,
  and exception fields (errorType, errorMessage, stackTrace, location)
- Update LoggerPatch to select formatter and log level based on env vars
- Fix LogFormatter to append newline for consistency
- Fix backtrace regex to handle both Ruby < 3.4 and 3.4+ formats
- Upgrade Dockerfile.rie base image to Ruby 3.4
- Relax bundler development dependency to >= 2.0
@anzheyazzz anzheyazzz requested a review from maxday March 10, 2026 22:55
progname: progname,
msg: msg2str(msg)
}
"#{formatted.encode('UTF-8')}\n"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we rather want to do formatted.encode('UTF-8', invalid: :replace, undef: :replace, replace: '�')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To not make bad characters fail @maxday ?

@@ -1,12 +1,65 @@
# frozen_string_literal: true

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IN this PR I see we are patching both Kernel puts and also logger -> Not this is not usual - For all runtimes we just choose 1 thing and then patch that. For nodejs it's console for python logger. Not sure if we should do both. This is becoming hard to follow. DO we need to patch Kernel.puts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants