Logging is an important part of software development that helps you to analyze the behavior of your applications. SAP Commerce comes pre-bundled with Apache Log4j 2.
Log4j 2 is the standard logging framework in SAP Commerce. It’s recommended that you use Simple Logging Facade for Java (SLF4J) as the logging API for Log4j 2:
Log levels
TRACE: typically used to provide detailed diagnostic information that can be used for troubleshooting and debugging. Compared toDEBUGmessages,TRACEmessages are more fine-grained and verbose.DEBUG: used to provide information that can be used to diagnose issues especially those related to program state.INFO: used to record events that indicate that the program is functioning normally.WARN: used to record potential issues in your application. They may not be critical but should be investigated.ERROR: records unexpected errors that occur during the operation of your application. In most cases, the error should be addressed as soon as possible to prevent further problems or outages.
Must to follow/Guideline
- Use common logging API inside the project e.g. we can all follow the Simple Logging Facade for Java (SLF4J) here. import org.slf4j.Logger;import org.slf4j.LoggerFactory ,final Logger slf4jLogger = LoggerFactory.getLogger(LoggingFrameworksBridgeTest.class);
- Use Different Log Levels appropriately: Utilize different log levels (DEBUG, INFO, WARN, ERROR) to categorize log messages based on their significance. Debug messages are for detailed debugging, while error messages indicate critical issues.
- Log Exception Stack Traces : Always use LOG.error(“Error message with context paramerts“,exception)
, LOG.error(“Error messase” +exception) 
- Secure Sensitive Information: We should try to avoid logging sensitive information e.g. Customer Email Address, Name, phone etc. If you want to log sensitive information then there should be mandatory masking of information. LOG.info(“Customer email id {}“,customer.getEmailId())

- Include Relevant Contextual Information: Include relevant contextual information such as user IDs, session IDs, and request/response details in the log messages. This can be immensely helpful when diagnosing specific issues. e.g. LOG.error(“Error during cart saving. Cart id {} and User id { } “,id,user-id, exception passed)

- Use Logger Naming Conventions/Logging Conventions, Use Descriptive Log Messages and Format Log Messages: Follow consistent naming conventions for loggers across your application. Ensure uniformity in logger names. This makes it easier to identify the source of log messages and manage logging configurations. Document your application’s logging conventions and practices. This is particularly useful for onboarding new team members and ensuring consistency across the codebase. Start using logging events. e.g. Order Placed. LOG.Info(“Order { } placed successfully“, orderId)
- Implement Contextual Logging: Utilize MDC (Mapped Diagnostic Context) or NDC (Nested Diagnostic Context) features provided by logging frameworks. These mechanisms allow you to track contextual information throughout the execution flow.
- Use Log Correlation IDs: Introduce correlation IDs to track requests or transactions across different components or services. This can help in tracing a single operation through various log entries.
- Log Early and Log Late: Place log messages at the beginning and end of critical methods or workflows. This helps in tracking the flow of execution and identifying potential bottlenecks.
- Avoid doing Concatenation log messages for logging.
- Avoid Excessive Logging: Event logging is very important but try not to log too much information.
- Business Validation Case: Try not to log errors when a validation error occurs. Validation errors are expected in our code.
Leave a comment