diff --git a/libraries/src/AWS.Lambda.Powertools.Logging/Logger.ExtraKeysLogs.cs b/libraries/src/AWS.Lambda.Powertools.Logging/Logger.ExtraKeysLogs.cs deleted file mode 100644 index be00722a5..000000000 --- a/libraries/src/AWS.Lambda.Powertools.Logging/Logger.ExtraKeysLogs.cs +++ /dev/null @@ -1,439 +0,0 @@ -/* - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - -using System; -using Microsoft.Extensions.Logging; - -namespace AWS.Lambda.Powertools.Logging; - -public partial class Logger -{ - #region ExtraKeys Logger Methods - - #region Debug - - /// - /// Formats and writes a debug log message. - /// - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogDebug(extraKeys, 0, exception, "Error while processing request from {Address}", address) - public static void LogDebug(T extraKeys, EventId eventId, Exception exception, string message, - params object[] args) where T : class - { - LoggerInstance.LogDebug(extraKeys, eventId, exception, message, args); - } - - /// - /// Formats and writes a debug log message. - /// - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogDebug(extraKeys, 0, "Processing request from {Address}", address) - public static void LogDebug(T extraKeys, EventId eventId, string message, params object[] args) where T : class - { - LoggerInstance.LogDebug(extraKeys, eventId, message, args); - } - - /// - /// Formats and writes a debug log message. - /// - /// Additional keys will be appended to the log entry. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogDebug(extraKeys, exception, "Error while processing request from {Address}", address) - public static void LogDebug(T extraKeys, Exception exception, string message, params object[] args) - where T : class - { - LoggerInstance.LogDebug(extraKeys, exception, message, args); - } - - /// - /// Formats and writes a debug log message. - /// - /// Additional keys will be appended to the log entry. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogDebug(extraKeys, "Processing request from {Address}", address) - public static void LogDebug(T extraKeys, string message, params object[] args) where T : class - { - LoggerInstance.LogDebug(extraKeys, message, args); - } - - #endregion - - #region Trace - - /// - /// Formats and writes a trace log message. - /// - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogTrace(extraKeys, 0, exception, "Error while processing request from {Address}", address) - public static void LogTrace(T extraKeys, EventId eventId, Exception exception, string message, - params object[] args) where T : class - { - LoggerInstance.LogTrace(extraKeys, eventId, exception, message, args); - } - - /// - /// Formats and writes a trace log message. - /// - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogTrace(extraKeys, 0, "Processing request from {Address}", address) - public static void LogTrace(T extraKeys, EventId eventId, string message, params object[] args) where T : class - { - LoggerInstance.LogTrace(extraKeys, eventId, message, args); - } - - /// - /// Formats and writes a trace log message. - /// - /// Additional keys will be appended to the log entry. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogTrace(extraKeys, exception, "Error while processing request from {Address}", address) - public static void LogTrace(T extraKeys, Exception exception, string message, params object[] args) - where T : class - { - LoggerInstance.LogTrace(extraKeys, exception, message, args); - } - - /// - /// Formats and writes a trace log message. - /// - /// Additional keys will be appended to the log entry. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogTrace(extraKeys, "Processing request from {Address}", address) - public static void LogTrace(T extraKeys, string message, params object[] args) where T : class - { - LoggerInstance.LogTrace(extraKeys, message, args); - } - - #endregion - - #region Information - - /// - /// Formats and writes an informational log message. - /// - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogInformation(extraKeys, 0, exception, "Error while processing request from {Address}", address) - public static void LogInformation(T extraKeys, EventId eventId, Exception exception, string message, - params object[] args) where T : class - { - LoggerInstance.LogInformation(extraKeys, eventId, exception, message, args); - } - - /// - /// Formats and writes an informational log message. - /// - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogInformation(extraKeys, 0, "Processing request from {Address}", address) - public static void LogInformation(T extraKeys, EventId eventId, string message, params object[] args) - where T : class - { - LoggerInstance.LogInformation(extraKeys, eventId, message, args); - } - - /// - /// Formats and writes an informational log message. - /// - /// Additional keys will be appended to the log entry. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogInformation(extraKeys, exception, "Error while processing request from {Address}", address) - public static void LogInformation(T extraKeys, Exception exception, string message, params object[] args) - where T : class - { - LoggerInstance.LogInformation(extraKeys, exception, message, args); - } - - /// - /// Formats and writes an informational log message. - /// - /// Additional keys will be appended to the log entry. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogInformation(extraKeys, "Processing request from {Address}", address) - public static void LogInformation(T extraKeys, string message, params object[] args) where T : class - { - LoggerInstance.LogInformation(extraKeys, message, args); - } - - #endregion - - #region Warning - - /// - /// Formats and writes a warning log message. - /// - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogWarning(extraKeys, 0, exception, "Error while processing request from {Address}", address) - public static void LogWarning(T extraKeys, EventId eventId, Exception exception, string message, - params object[] args) where T : class - { - LoggerInstance.LogWarning(extraKeys, eventId, exception, message, args); - } - - /// - /// Formats and writes a warning log message. - /// - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogWarning(extraKeys, 0, "Processing request from {Address}", address) - public static void LogWarning(T extraKeys, EventId eventId, string message, params object[] args) where T : class - { - LoggerInstance.LogWarning(extraKeys, eventId, message, args); - } - - /// - /// Formats and writes a warning log message. - /// - /// Additional keys will be appended to the log entry. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogWarning(extraKeys, exception, "Error while processing request from {Address}", address) - public static void LogWarning(T extraKeys, Exception exception, string message, params object[] args) - where T : class - { - LoggerInstance.LogWarning(extraKeys, exception, message, args); - } - - /// - /// Formats and writes a warning log message. - /// - /// Additional keys will be appended to the log entry. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogWarning(extraKeys, "Processing request from {Address}", address) - public static void LogWarning(T extraKeys, string message, params object[] args) where T : class - { - LoggerInstance.LogWarning(extraKeys, message, args); - } - - #endregion - - #region Error - - /// - /// Formats and writes an error log message. - /// - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogError(extraKeys, 0, exception, "Error while processing request from {Address}", address) - public static void LogError(T extraKeys, EventId eventId, Exception exception, string message, - params object[] args) where T : class - { - LoggerInstance.LogError(extraKeys, eventId, exception, message, args); - } - - /// - /// Formats and writes an error log message. - /// - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogError(extraKeys, 0, "Processing request from {Address}", address) - public static void LogError(T extraKeys, EventId eventId, string message, params object[] args) where T : class - { - LoggerInstance.LogError(extraKeys, eventId, message, args); - } - - /// - /// Formats and writes an error log message. - /// - /// Additional keys will be appended to the log entry. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogError(extraKeys, exception, "Error while processing request from {Address}", address) - public static void LogError(T extraKeys, Exception exception, string message, params object[] args) - where T : class - { - LoggerInstance.LogError(extraKeys, exception, message, args); - } - - /// - /// Formats and writes an error log message. - /// - /// Additional keys will be appended to the log entry. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogError(extraKeys, "Processing request from {Address}", address) - public static void LogError(T extraKeys, string message, params object[] args) where T : class - { - LoggerInstance.LogError(extraKeys, message, args); - } - - #endregion - - #region Critical - - /// - /// Formats and writes a critical log message. - /// - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogCritical(extraKeys, 0, exception, "Error while processing request from {Address}", address) - public static void LogCritical(T extraKeys, EventId eventId, Exception exception, string message, - params object[] args) where T : class - { - LoggerInstance.LogCritical(extraKeys, eventId, exception, message, args); - } - - /// - /// Formats and writes a critical log message. - /// - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogCritical(extraKeys, 0, "Processing request from {Address}", address) - public static void LogCritical(T extraKeys, EventId eventId, string message, params object[] args) - where T : class - { - LoggerInstance.LogCritical(extraKeys, eventId, message, args); - } - - /// - /// Formats and writes a critical log message. - /// - /// Additional keys will be appended to the log entry. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogCritical(extraKeys, exception, "Error while processing request from {Address}", address) - public static void LogCritical(T extraKeys, Exception exception, string message, params object[] args) - where T : class - { - LoggerInstance.LogCritical(extraKeys, exception, message, args); - } - - /// - /// Formats and writes a critical log message. - /// - /// Additional keys will be appended to the log entry. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogCritical(extraKeys, "Processing request from {Address}", address) - public static void LogCritical(T extraKeys, string message, params object[] args) where T : class - { - LoggerInstance.LogCritical(extraKeys, message, args); - } - - #endregion - - #region Log - - /// - /// Formats and writes a log message at the specified log level. - /// - /// Entry will be written on this level. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.Log(LogLevel.Information, extraKeys, 0, exception, "Error while processing request from {Address}", address) - public static void Log(LogLevel logLevel, T extraKeys, EventId eventId, Exception exception, string message, - params object[] args) where T : class - { - LoggerInstance.Log(logLevel, extraKeys, eventId, exception, message, args); - } - - /// - /// Formats and writes a log message at the specified log level. - /// - /// Entry will be written on this level. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.Log(LogLevel.Information, extraKeys, 0, "Processing request from {Address}", address) - public static void Log(LogLevel logLevel, T extraKeys, EventId eventId, string message, params object[] args) - where T : class - { - LoggerInstance.Log(logLevel, extraKeys, eventId, message, args); - } - - /// - /// Formats and writes a log message at the specified log level. - /// - /// Entry will be written on this level. - /// Additional keys will be appended to the log entry. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.Log(LogLevel.Information, extraKeys, exception, "Error while processing request from {Address}", address) - public static void Log(LogLevel logLevel, T extraKeys, Exception exception, string message, params object[] args) - where T : class - { - LoggerInstance.Log(logLevel, extraKeys, exception, message, args); - } - - /// - /// Formats and writes a log message at the specified log level. - /// - /// Entry will be written on this level. - /// Additional keys will be appended to the log entry. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.Log(LogLevel.Information, extraKeys, "Processing request from {Address}", address) - public static void Log(LogLevel logLevel, T extraKeys, string message, params object[] args) where T : class - { - LoggerInstance.Log(logLevel, extraKeys, message, args); - } - - #endregion - - #endregion -} \ No newline at end of file diff --git a/libraries/src/AWS.Lambda.Powertools.Logging/PowertoolsLoggerExtensions.cs b/libraries/src/AWS.Lambda.Powertools.Logging/PowertoolsLoggerExtensions.cs index e3ca6780d..2f131362c 100644 --- a/libraries/src/AWS.Lambda.Powertools.Logging/PowertoolsLoggerExtensions.cs +++ b/libraries/src/AWS.Lambda.Powertools.Logging/PowertoolsLoggerExtensions.cs @@ -184,477 +184,7 @@ public static void Log(this ILogger logger, LogLevel logLevel, Exception excepti } #endregion - - #region ExtraKeys Logger Extentions - - #region Debug - - /// - /// Formats and writes a debug log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogDebug(extraKeys, 0, exception, "Error while processing request from {Address}", address) - public static void LogDebug(this ILogger logger, T extraKeys, EventId eventId, Exception exception, - string message, params object[] args) where T : class - { - Log(logger, LogLevel.Debug, extraKeys, eventId, exception, message, args); - } - - /// - /// Formats and writes a debug log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogDebug(extraKeys, 0, "Processing request from {Address}", address) - public static void LogDebug(this ILogger logger, T extraKeys, EventId eventId, string message, - params object[] args) where T : class - { - Log(logger, LogLevel.Debug, extraKeys, eventId, message, args); - } - - /// - /// Formats and writes a debug log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogDebug(extraKeys, exception, "Error while processing request from {Address}", address) - public static void LogDebug(this ILogger logger, T extraKeys, Exception exception, string message, - params object[] args) where T : class - { - Log(logger, LogLevel.Debug, extraKeys, exception, message, args); - } - - /// - /// Formats and writes a debug log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogDebug(extraKeys, "Processing request from {Address}", address) - public static void LogDebug(this ILogger logger, T extraKeys, string message, params object[] args) - where T : class - { - Log(logger, LogLevel.Debug, extraKeys, message, args); - } - - #endregion - - #region Trace - - /// - /// Formats and writes a trace log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogTrace(extraKeys, 0, exception, "Error while processing request from {Address}", address) - public static void LogTrace(this ILogger logger, T extraKeys, EventId eventId, Exception exception, - string message, params object[] args) where T : class - { - Log(logger, LogLevel.Trace, extraKeys, eventId, exception, message, args); - } - - /// - /// Formats and writes a trace log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogTrace(extraKeys, 0, "Processing request from {Address}", address) - public static void LogTrace(this ILogger logger, T extraKeys, EventId eventId, string message, - params object[] args) where T : class - { - Log(logger, LogLevel.Trace, extraKeys, eventId, message, args); - } - - /// - /// Formats and writes a trace log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogTrace(extraKeys, exception, "Error while processing request from {Address}", address) - public static void LogTrace(this ILogger logger, T extraKeys, Exception exception, string message, - params object[] args) where T : class - { - Log(logger, LogLevel.Trace, extraKeys, exception, message, args); - } - - /// - /// Formats and writes a trace log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogTrace(extraKeys, "Processing request from {Address}", address) - public static void LogTrace(this ILogger logger, T extraKeys, string message, params object[] args) - where T : class - { - Log(logger, LogLevel.Trace, extraKeys, message, args); - } - - #endregion - - #region Information - - /// - /// Formats and writes an informational log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogInformation(extraKeys, 0, exception, "Error while processing request from {Address}", address) - public static void LogInformation(this ILogger logger, T extraKeys, EventId eventId, Exception exception, - string message, params object[] args) where T : class - { - Log(logger, LogLevel.Information, extraKeys, eventId, exception, message, args); - } - - /// - /// Formats and writes an informational log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogInformation(extraKeys, 0, "Processing request from {Address}", address) - public static void LogInformation(this ILogger logger, T extraKeys, EventId eventId, string message, - params object[] args) where T : class - { - Log(logger, LogLevel.Information, extraKeys, eventId, message, args); - } - - /// - /// Formats and writes an informational log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogInformation(extraKeys, exception, "Error while processing request from {Address}", address) - public static void LogInformation(this ILogger logger, T extraKeys, Exception exception, string message, - params object[] args) where T : class - { - Log(logger, LogLevel.Information, extraKeys, exception, message, args); - } - - /// - /// Formats and writes an informational log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogInformation(extraKeys, "Processing request from {Address}", address) - public static void LogInformation(this ILogger logger, T extraKeys, string message, params object[] args) - where T : class - { - Log(logger, LogLevel.Information, extraKeys, message, args); - } - - #endregion - - #region Warning - - /// - /// Formats and writes a warning log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogWarning(extraKeys, 0, exception, "Error while processing request from {Address}", address) - public static void LogWarning(this ILogger logger, T extraKeys, EventId eventId, Exception exception, - string message, params object[] args) where T : class - { - Log(logger, LogLevel.Warning, extraKeys, eventId, exception, message, args); - } - - /// - /// Formats and writes a warning log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogWarning(extraKeys, 0, "Processing request from {Address}", address) - public static void LogWarning(this ILogger logger, T extraKeys, EventId eventId, string message, - params object[] args) where T : class - { - Log(logger, LogLevel.Warning, extraKeys, eventId, message, args); - } - - /// - /// Formats and writes a warning log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogWarning(extraKeys, exception, "Error while processing request from {Address}", address) - public static void LogWarning(this ILogger logger, T extraKeys, Exception exception, string message, - params object[] args) where T : class - { - Log(logger, LogLevel.Warning, extraKeys, exception, message, args); - } - - /// - /// Formats and writes a warning log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogWarning(extraKeys, "Processing request from {Address}", address) - public static void LogWarning(this ILogger logger, T extraKeys, string message, params object[] args) - where T : class - { - Log(logger, LogLevel.Warning, extraKeys, message, args); - } - - #endregion - - #region Error - - /// - /// Formats and writes an error log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogError(extraKeys, 0, exception, "Error while processing request from {Address}", address) - public static void LogError(this ILogger logger, T extraKeys, EventId eventId, Exception exception, - string message, params object[] args) where T : class - { - Log(logger, LogLevel.Error, extraKeys, eventId, exception, message, args); - } - - /// - /// Formats and writes an error log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogError(extraKeys, 0, "Processing request from {Address}", address) - public static void LogError(this ILogger logger, T extraKeys, EventId eventId, string message, - params object[] args) where T : class - { - Log(logger, LogLevel.Error, extraKeys, eventId, message, args); - } - - /// - /// Formats and writes an error log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogError(extraKeys, exception, "Error while processing request from {Address}", address) - public static void LogError(this ILogger logger, T extraKeys, Exception exception, string message, - params object[] args) where T : class - { - Log(logger, LogLevel.Error, extraKeys, exception, message, args); - } - - /// - /// Formats and writes an error log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogError(extraKeys, "Processing request from {Address}", address) - public static void LogError(this ILogger logger, T extraKeys, string message, params object[] args) - where T : class - { - Log(logger, LogLevel.Error, extraKeys, message, args); - } - - #endregion - - #region Critical - - /// - /// Formats and writes a critical log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogCritical(extraKeys, 0, exception, "Error while processing request from {Address}", address) - public static void LogCritical(this ILogger logger, T extraKeys, EventId eventId, Exception exception, - string message, params object[] args) where T : class - { - Log(logger, LogLevel.Critical, extraKeys, eventId, exception, message, args); - } - - /// - /// Formats and writes a critical log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogCritical(extraKeys, 0, "Processing request from {Address}", address) - public static void LogCritical(this ILogger logger, T extraKeys, EventId eventId, string message, - params object[] args) where T : class - { - Log(logger, LogLevel.Critical, extraKeys, eventId, message, args); - } - - /// - /// Formats and writes a critical log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogCritical(extraKeys, exception, "Error while processing request from {Address}", address) - public static void LogCritical(this ILogger logger, T extraKeys, Exception exception, string message, - params object[] args) where T : class - { - Log(logger, LogLevel.Critical, extraKeys, exception, message, args); - } - - /// - /// Formats and writes a critical log message. - /// - /// The to write to. - /// Additional keys will be appended to the log entry. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.LogCritical(extraKeys, "Processing request from {Address}", address) - public static void LogCritical(this ILogger logger, T extraKeys, string message, params object[] args) - where T : class - { - Log(logger, LogLevel.Critical, extraKeys, message, args); - } - - #endregion - - #region Log - - /// - /// Formats and writes a log message at the specified log level. - /// - /// The to write to. - /// Entry will be written on this level. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.Log(LogLevel.Information, extraKeys, 0, exception, "Error while processing request from {Address}", address) - public static void Log(this ILogger logger, LogLevel logLevel, T extraKeys, EventId eventId, Exception exception, - string message, params object[] args) where T : class - { - if (extraKeys is Exception ex && exception is null) - logger.Log(logLevel, eventId, ex, message, args); - else if (extraKeys is not null) - using (logger.BeginScope(extraKeys)) - logger.Log(logLevel, eventId, exception, message, args); - else - logger.Log(logLevel, eventId, exception, message, args); - } - - /// - /// Formats and writes a log message at the specified log level. - /// - /// The to write to. - /// Entry will be written on this level. - /// Additional keys will be appended to the log entry. - /// The event id associated with the log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.Log(LogLevel.Information, extraKeys, 0, "Processing request from {Address}", address) - public static void Log(this ILogger logger, LogLevel logLevel, T extraKeys, EventId eventId, string message, - params object[] args) where T : class - { - Log(logger, logLevel, extraKeys, eventId, null, message, args); - } - - /// - /// Formats and writes a log message at the specified log level. - /// - /// The to write to. - /// Entry will be written on this level. - /// Additional keys will be appended to the log entry. - /// The exception to log. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.Log(LogLevel.Information, extraKeys, exception, "Error while processing request from {Address}", address) - public static void Log(this ILogger logger, LogLevel logLevel, T extraKeys, Exception exception, string message, - params object[] args) where T : class - { - Log(logger, logLevel, extraKeys, 0, exception, message, args); - } - - /// - /// Formats and writes a log message at the specified log level. - /// - /// The to write to. - /// Entry will be written on this level. - /// Additional keys will be appended to the log entry. - /// Format string of the log message in message template format. Example: "User {User} logged in from {Address}" - /// An object array that contains zero or more objects to format. - /// logger.Log(LogLevel.Information, extraKeys, "Processing request from {Address}", address) - public static void Log(this ILogger logger, LogLevel logLevel, T extraKeys, string message, params object[] args) - where T : class - { - try - { - Log(logger, logLevel, extraKeys, 0, null, message, args); - } - catch (Exception e) - { - logger.Log(LogLevel.Error, 0, e, "Powertools internal error"); - } - } - - #endregion - - #endregion - - + /// /// Appending additional key to the log context. /// diff --git a/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Formatter/LogFormatterTest.cs b/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Formatter/LogFormatterTest.cs index 37f0fd836..811216fd5 100644 --- a/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Formatter/LogFormatterTest.cs +++ b/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Formatter/LogFormatterTest.cs @@ -25,6 +25,7 @@ using AWS.Lambda.Powertools.Logging.Internal; using AWS.Lambda.Powertools.Logging.Serializers; using AWS.Lambda.Powertools.Logging.Tests.Handlers; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using NSubstitute; using NSubstitute.ExceptionExtensions; @@ -193,7 +194,7 @@ public void Log_WhenCustomFormatter_LogsCustomFormat() }; // Act - logger.LogInformation(scopeExtraKeys, message); + logger.LogInformation(message, scopeExtraKeys); // Assert logFormatter.Received(1).FormatLogEntry(Arg.Is diff --git a/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Formatter/LogFormattingTests.cs b/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Formatter/LogFormattingTests.cs index d5effd4a1..46a5a459e 100644 --- a/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Formatter/LogFormattingTests.cs +++ b/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Formatter/LogFormattingTests.cs @@ -502,10 +502,9 @@ public void Should_Log_Multiple_Formats_No_Duplicates() TimeStamp = "FakeTime" }; - Logger.LogInformation(user, "{Name} and is {Age} years old", new object[]{user.Name, user.Age}); - Assert.Contains("\"first_name\":\"John\"", output.ToString()); - Assert.Contains("\"last_name\":\"Doe\"", output.ToString()); - Assert.Contains("\"age\":42", output.ToString()); + Logger.LogInformation("{Name} is {Age} years old", user.Name, user.Age); + + Assert.Contains("\"message\":\"John Doe is 42 years old\"", output.ToString()); Assert.Contains("\"name\":\"AWS.Lambda.Powertools.Logging.Logger\"", output.ToString()); // does not override name output.Clear(); @@ -564,16 +563,13 @@ public void Should_Log_Multiple_Formats() Age = 42 }; - Logger.LogInformation(user, "{Name} is {Age} years old", new object[]{user.FirstName, user.Age}); + Logger.LogInformation("{Name} is {Age} years old", user.FirstName, user.Age); var logOutput = output.ToString(); Assert.Contains("\"level\":\"Information\"", logOutput); Assert.Contains("\"message\":\"John is 42 years old\"", logOutput); Assert.Contains("\"service\":\"log-level-test-service\"", logOutput); Assert.Contains("\"name\":\"AWS.Lambda.Powertools.Logging.Logger\"", logOutput); - Assert.Contains("\"first_name\":\"John\"", logOutput); - Assert.Contains("\"last_name\":\"Doe\"", logOutput); - Assert.Contains("\"age\":42", logOutput); output.Clear(); @@ -630,8 +626,63 @@ public void Should_Log_Multiple_Formats() Assert.Contains("\"level\":\"Information\"", logOutput); Assert.DoesNotContain("\"level\":\"fakeLevel\"", logOutput); + output.Clear(); + + Logger.LogInformation("{Name} is {Age} years old and {@user}", user.FirstName, user.Age, user); + + logOutput = output.ToString(); + + Assert.Contains("\"message\":\"John is 42 years old and Doe, John (42)\"", logOutput); + // Verify serialized user object with all properties + Assert.Contains("\"user\":{", logOutput); + Assert.Contains("\"first_name\":\"John\"", logOutput); + Assert.Contains("\"last_name\":\"Doe\"", logOutput); + Assert.Contains("\"age\":42", logOutput); + Assert.Contains("\"name\":\"John Doe\"", logOutput); + Assert.Contains("\"time_stamp\":null", logOutput); + Assert.Contains("}", logOutput); + _output.WriteLine(logOutput); + } + + [Fact] + public void TestMessageTemplateFormatting() + { + var output = new TestLoggerOutput(); + var logger = LoggerFactory.Create(builder => + { + builder.AddPowertoolsLogger(config => + { + config.Service = "template-format-service"; + config.MinimumLogLevel = LogLevel.Debug; + config.LoggerOutputCase = LoggerOutputCase.SnakeCase; + config.LogOutput = output; + }); + }).CreatePowertoolsLogger(); + + // Simple template with one parameter + logger.LogInformation("This is a test with {param}", "Hello"); + + var logOutput = output.ToString(); + _output.WriteLine(logOutput); + + // Verify full formatted message appears correctly + Assert.Contains("\"message\":\"This is a test with Hello\"", logOutput); + // Verify parameter is also included separately + Assert.Contains("\"param\":\"Hello\"", logOutput); + + output.Clear(); + + // Multiple parameters + logger.LogInformation("Test with {first} and {second}", "One", "Two"); + + logOutput = output.ToString(); + _output.WriteLine(logOutput); + // Verify message with multiple parameters + Assert.Contains("\"message\":\"Test with One and Two\"", logOutput); + Assert.Contains("\"first\":\"One\"", logOutput); + Assert.Contains("\"second\":\"Two\"", logOutput); } public class ParentClass diff --git a/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Handlers/ExceptionFunctionHandler.cs b/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Handlers/ExceptionFunctionHandler.cs index 170f2a922..56dfcc872 100644 --- a/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Handlers/ExceptionFunctionHandler.cs +++ b/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Handlers/ExceptionFunctionHandler.cs @@ -34,14 +34,14 @@ public string HandlerLoggerForExceptions(string input, ILambdaContext context) Logger.LogDebug("Hello {input}", input); Logger.LogTrace("Hello {input}", input); - Logger.LogInformation("Testing with parameter Log Information Method {company}", new[] { "AWS" }); + Logger.LogInformation("Testing with parameter Log Information Method {company}", "AWS" ); var customKeys = new Dictionary { {"test1", "value1"}, {"test2", "value2"} }; - Logger.LogInformation(customKeys, "Retrieved data for city {cityName} with count {company}", "AWS"); + Logger.LogInformation("Retrieved data for city {cityName} with count {company}", "AWS", customKeys); Logger.AppendKey("aws",1); Logger.AppendKey("aws",3); @@ -52,10 +52,4 @@ public string HandlerLoggerForExceptions(string input, ILambdaContext context) return "OK"; } - - [Logging(LogEvent = true)] - public string HandleOk(string input) - { - return input.ToUpper(CultureInfo.InvariantCulture); - } } \ No newline at end of file diff --git a/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/PowertoolsLoggerTest.cs b/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/PowertoolsLoggerTest.cs index 756e7234b..7f3d27b01 100644 --- a/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/PowertoolsLoggerTest.cs +++ b/libraries/tests/AWS.Lambda.Powertools.Logging.Tests/PowertoolsLoggerTest.cs @@ -811,7 +811,7 @@ public void Log_WhenExtraKeysIsObjectDictionary_AppendExtraKeys(LogLevel logLeve // Arrange var loggerName = Guid.NewGuid().ToString(); var service = Guid.NewGuid().ToString(); - var message = Guid.NewGuid().ToString(); + var message = "{@keys}"; var configurations = Substitute.For(); configurations.Service.Returns(service); @@ -836,29 +836,29 @@ public void Log_WhenExtraKeysIsObjectDictionary_AppendExtraKeys(LogLevel logLeve if (logMethod) { - logger.Log(logLevel, scopeKeys, message); + logger.Log(logLevel, message,scopeKeys); } else { switch (logLevel) { case LogLevel.Trace: - logger.LogTrace(scopeKeys, message); + logger.LogTrace(message,scopeKeys); break; case LogLevel.Debug: - logger.LogDebug(scopeKeys, message); + logger.LogDebug(message,scopeKeys); break; case LogLevel.Information: - logger.LogInformation(scopeKeys, message); + logger.LogInformation(message,scopeKeys); break; case LogLevel.Warning: - logger.LogWarning(scopeKeys, message); + logger.LogWarning(message,scopeKeys); break; case LogLevel.Error: - logger.LogError(scopeKeys, message); + logger.LogError(message,scopeKeys); break; case LogLevel.Critical: - logger.LogCritical(scopeKeys, message); + logger.LogCritical(message,scopeKeys); break; case LogLevel.None: break; @@ -895,7 +895,7 @@ public void Log_WhenExtraKeysIsStringDictionary_AppendExtraKeys(LogLevel logLeve // Arrange var loggerName = Guid.NewGuid().ToString(); var service = Guid.NewGuid().ToString(); - var message = Guid.NewGuid().ToString(); + var message = "{@keys}"; var configurations = Substitute.For(); configurations.Service.Returns(service); @@ -921,29 +921,29 @@ public void Log_WhenExtraKeysIsStringDictionary_AppendExtraKeys(LogLevel logLeve if (logMethod) { - logger.Log(logLevel, scopeKeys, message); + logger.Log(logLevel, message,scopeKeys); } else { switch (logLevel) { case LogLevel.Trace: - logger.LogTrace(scopeKeys, message); + logger.LogTrace(message,scopeKeys); break; case LogLevel.Debug: - logger.LogDebug(scopeKeys, message); + logger.LogDebug(message,scopeKeys); break; case LogLevel.Information: - logger.LogInformation(scopeKeys, message); + logger.LogInformation(message,scopeKeys); break; case LogLevel.Warning: - logger.LogWarning(scopeKeys, message); + logger.LogWarning(message,scopeKeys); break; case LogLevel.Error: - logger.LogError(scopeKeys, message); + logger.LogError(message,scopeKeys); break; case LogLevel.Critical: - logger.LogCritical(scopeKeys, message); + logger.LogCritical(message,scopeKeys); break; case LogLevel.None: break; @@ -980,7 +980,7 @@ public void Log_WhenExtraKeysAsObject_AppendExtraKeys(LogLevel logLevel, bool lo // Arrange var loggerName = Guid.NewGuid().ToString(); var service = Guid.NewGuid().ToString(); - var message = Guid.NewGuid().ToString(); + var message = "{@keys}"; var configurations = Substitute.For(); configurations.Service.Returns(service); @@ -1006,29 +1006,29 @@ public void Log_WhenExtraKeysAsObject_AppendExtraKeys(LogLevel logLevel, bool lo if (logMethod) { - logger.Log(logLevel, scopeKeys, message); + logger.Log(logLevel, message, scopeKeys); } else { switch (logLevel) { case LogLevel.Trace: - logger.LogTrace(scopeKeys, message); + logger.LogTrace(message,scopeKeys); break; case LogLevel.Debug: - logger.LogDebug(scopeKeys, message); + logger.LogDebug(message,scopeKeys); break; case LogLevel.Information: - logger.LogInformation(scopeKeys, message); + logger.LogInformation(message,scopeKeys); break; case LogLevel.Warning: - logger.LogWarning(scopeKeys, message); + logger.LogWarning(message,scopeKeys); break; case LogLevel.Error: - logger.LogError(scopeKeys, message); + logger.LogError(message,scopeKeys); break; case LogLevel.Critical: - logger.LogCritical(scopeKeys, message); + logger.LogCritical(message,scopeKeys); break; case LogLevel.None: break;