Skip to main content

Configuring and Writing Logs

To write strict logging entries directly from a Spring Class, you instantiate a generic internal Logger.

Writing Logs in Java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class UserService {

// Instantiate the SLF4J logger targeting the class name securely
private static final Logger logger = LoggerFactory.getLogger(UserService.class);

public void processUser(String username) {
logger.info("Initializing processing for user: {}", username);

try {
// Processing logic
} catch (Exception e) {
logger.error("Severe failure occurred processing user", e);
}
}
}

Note: If you use the Project Lombok dependency, you can simply place @Slf4j above the class definition, and Lombok automatically injects the log variable instance internally.

Modifying Spring Properties

You can dictate global logging configuration utilizing src/main/resources/application.properties.

To enforce DEBUG logs exclusively for your com.codenbuild package while ignoring generic framework debug spam:

logging.level.com.codenbuild=DEBUG
logging.level.org.springframework=WARN

Logging to a File

By default, Spring completely restricts logging strictly to the Console view. To generate physical logs required for external monitoring platforms (like Splunk or Datadog), you define an output configuration natively.

# System file configuration
logging.file.name=logs/codenbuild.log
logging.logback.rollingpolicy.max-file-size=10MB
logging.logback.rollingpolicy.max-history=30

This forces Spring to dump output logs continuously targeting logs/codenbuild.log, archiving them daily or whenever the file breaches the 10MB data limit.