-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[alc] ContextLogger changes in RIC (#436)
* [alc] ContextLogger changes in RIC --------- Co-authored-by: Daniel Torok <torokd@amazon.com>
- Loading branch information
Showing
22 changed files
with
532 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...n/java/com/amazonaws/services/lambda/runtime/api/client/logging/AbstractLambdaLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. */ | ||
|
||
package com.amazonaws.services.lambda.runtime.api.client.logging; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import com.amazonaws.services.lambda.runtime.api.client.api.LambdaContext; | ||
import com.amazonaws.services.lambda.runtime.LambdaLogger; | ||
import com.amazonaws.services.lambda.runtime.logging.LogFormat; | ||
import com.amazonaws.services.lambda.runtime.logging.LogLevel; | ||
|
||
/** | ||
* Provides default implementation of the convenience logger functions. | ||
* When extending AbstractLambdaLogger, only one function has to be overridden: | ||
* void logMessage(byte[] message, LogLevel logLevel); | ||
*/ | ||
public abstract class AbstractLambdaLogger implements LambdaLogger { | ||
private final LogFiltering logFiltering; | ||
private final LogFormatter logFormatter; | ||
protected final LogFormat logFormat; | ||
|
||
public AbstractLambdaLogger(LogLevel logLevel, LogFormat logFormat) { | ||
this.logFiltering = new LogFiltering(logLevel); | ||
|
||
this.logFormat = logFormat; | ||
if (logFormat == LogFormat.JSON) { | ||
logFormatter = new JsonLogFormatter(); | ||
} else { | ||
logFormatter = new TextLogFormatter(); | ||
} | ||
} | ||
|
||
protected abstract void logMessage(byte[] message, LogLevel logLevel); | ||
|
||
protected void logMessage(String message, LogLevel logLevel) { | ||
logMessage(message.getBytes(UTF_8), logLevel); | ||
} | ||
|
||
@Override | ||
public void log(String message, LogLevel logLevel) { | ||
if (logFiltering.isEnabled(logLevel)) { | ||
this.logMessage(logFormatter.format(message, logLevel), logLevel); | ||
} | ||
} | ||
|
||
@Override | ||
public void log(byte[] message, LogLevel logLevel) { | ||
if (logFiltering.isEnabled(logLevel)) { | ||
// there is no formatting for byte[] messages | ||
this.logMessage(message, logLevel); | ||
} | ||
} | ||
|
||
@Override | ||
public void log(String message) { | ||
this.log(message, LogLevel.UNDEFINED); | ||
} | ||
|
||
@Override | ||
public void log(byte[] message) { | ||
this.log(message, LogLevel.UNDEFINED); | ||
} | ||
|
||
public void setLambdaContext(LambdaContext lambdaContext) { | ||
this.logFormatter.setLambdaContext(lambdaContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
.../main/java/com/amazonaws/services/lambda/runtime/api/client/logging/JsonLogFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. */ | ||
|
||
package com.amazonaws.services.lambda.runtime.api.client.logging; | ||
|
||
import com.amazonaws.services.lambda.runtime.api.client.api.LambdaContext; | ||
import com.amazonaws.services.lambda.runtime.logging.LogLevel; | ||
import com.amazonaws.services.lambda.runtime.serialization.PojoSerializer; | ||
import com.amazonaws.services.lambda.runtime.serialization.factories.GsonFactory; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class JsonLogFormatter implements LogFormatter { | ||
private final PojoSerializer<StructuredLogMessage> serializer = GsonFactory.getInstance().getSerializer(StructuredLogMessage.class); | ||
private LambdaContext lambdaContext; | ||
|
||
private static final DateTimeFormatter dateFormatter = | ||
DateTimeFormatter | ||
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") | ||
.withZone(ZoneId.of("UTC")); | ||
|
||
@Override | ||
public String format(String message, LogLevel logLevel) { | ||
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
StructuredLogMessage msg = createLogMessage(message, logLevel); | ||
serializer.toJson(msg, stream); | ||
stream.write('\n'); | ||
return new String(stream.toByteArray(), StandardCharsets.UTF_8); | ||
} | ||
|
||
private StructuredLogMessage createLogMessage(String message, LogLevel logLevel) { | ||
StructuredLogMessage msg = new StructuredLogMessage(); | ||
msg.timestamp = dateFormatter.format(LocalDateTime.now()); | ||
msg.message = message; | ||
msg.level = logLevel; | ||
|
||
if (lambdaContext != null) { | ||
msg.AWSRequestId = lambdaContext.getAwsRequestId(); | ||
} | ||
return msg; | ||
} | ||
|
||
|
||
/** | ||
* Function to set the context for every invocation. | ||
* This way the logger will be able to attach additional information to the log packet. | ||
*/ | ||
@Override | ||
public void setLambdaContext(LambdaContext context) { | ||
this.lambdaContext = context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
.../src/main/java/com/amazonaws/services/lambda/runtime/api/client/logging/LogFiltering.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. */ | ||
|
||
package com.amazonaws.services.lambda.runtime.api.client.logging; | ||
|
||
import com.amazonaws.services.lambda.runtime.logging.LogLevel; | ||
|
||
public class LogFiltering { | ||
private final LogLevel minimumLogLevel; | ||
|
||
public LogFiltering(LogLevel minimumLogLevel) { | ||
this.minimumLogLevel = minimumLogLevel; | ||
} | ||
|
||
boolean isEnabled(LogLevel logLevel) { | ||
return (logLevel == LogLevel.UNDEFINED || logLevel.ordinal() >= minimumLogLevel.ordinal()); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
.../src/main/java/com/amazonaws/services/lambda/runtime/api/client/logging/LogFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. */ | ||
|
||
package com.amazonaws.services.lambda.runtime.api.client.logging; | ||
|
||
import com.amazonaws.services.lambda.runtime.api.client.api.LambdaContext; | ||
import com.amazonaws.services.lambda.runtime.logging.LogLevel; | ||
|
||
public interface LogFormatter { | ||
String format(String message, LogLevel logLevel); | ||
|
||
default void setLambdaContext(LambdaContext context) { | ||
}; | ||
} |
Oops, something went wrong.