Skip to content

monitoring and logging practices

Shelson Ferrari edited this page Jul 23, 2024 · 5 revisions

Monitoring and Logging Practices

Logging Implementation

Logs are essential for monitoring and debugging the application. They provide valuable information about the application's behavior in real-time and help identify issues.

Log Configuration in Spring Boot

In Spring Boot, logs can be configured in the application.properties file. Here are some common configurations:

logging.level.org.springframework.web=ERROR
logging.level.org.springframework.boot=ERROR
logging.level.root=INFO
logging.file.name=logs/java_base.log
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n

Utilização de Logs no Código

Para adicionar logs ao código, foi utilizado o framework SLF4J juntamente com a implementação do Logback, que é a implementação padrão do Spring Boot.

Exemplo de utilização de logs em um serviço:

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

@Service
public class CurrencyConversionService {

    private static final Logger logger = LoggerFactory.getLogger(CurrencyConversionService.class);

    public void convertCurrency(Currency sourceCurrency, Currency targetCurrency, double amount) {
        logger.info("Iniciando a conversão de moeda de {} para {}", sourceCurrency, targetCurrency);

        // Lógica de conversão de moeda

        logger.info("Conversão concluída com sucesso");
    }
}

Best Development Practices

Following best development practices is crucial to ensure code quality, maintainability, and scalability. Here are some recommended best practices:

Clean and Readable Code

  • Descriptive variable and method names: Use names that clearly describe the purpose of the variable or method.
  • Useful comments: Add comments only where necessary to explain complex logic or design decisions.
  • Code organization: Maintain a consistent code structure, breaking it down into smaller, cohesive methods and classes.

SOLID Principles

  • Single Responsibility Principle (SRP): A class should have only one responsibility.
  • Open/Closed Principle (OCP): Classes should be open for extension but closed for modification.
  • Liskov Substitution Principle (LSP): Subtypes must be substitutable for their base types.
  • Interface Segregation Principle (ISP): Many specific interfaces are better than one general interface.
  • Dependency Inversion Principle (DIP): Depend on abstractions, not on concrete implementations.

Exception Handling

Custom exceptions: Create custom exceptions for specific situations, making error handling easier. Clear error messages: Provide clear and informative error messages.

Example of a custom exception:

public class ResourceNotFoundException extends RuntimeException {
    public ResourceNotFoundException(String message) {
        super(message);
    }
}

Unit Tests

Test coverage: Ensure good test coverage for critical parts of the code. Independent tests: Tests should be independent of each other and the environment. Mocking: Use mocking frameworks like Mockito to test code units in isolation.

Version Control

Frequent commits: Make frequent commits with descriptive messages. Feature branches: Use separate branches for each new feature or bug fix. Pull Requests: Use pull requests to review code before merging it into the main branch.

Documentation

Document the code: Use JavaDoc to document classes and methods. Project documentation: Keep an updated README.md with project setup and execution instructions.

Conclusion

By following these logging and development practices, you can ensure that your code is easier to maintain, debug, and scale. Implementing logs and adhering to good development practices are essential for the long-term success of any software project.


Wiki Menu

Wiki Main Page

1. Introduction to the Project

  • Overview: Presentation of the project, highlighting its purpose and the context in which it is embedded.
  • Project Objectives: Enumeration of the main objectives that the project aims to achieve.
  • Scope and Functionalities: Description of the main functionalities offered by the project and its scope of operation.

2. Configuration and Installation

3. Project Structure

  • Folder Structure: Description of the organization of the project directories.
  • Project Architecture: Explanation of the architecture used, including design patterns and technical decisions.

4. Development

  • Development Flow: Description of the development process adopted, including planning, coding, and review stages.
  • Apache Camel Integration: Guide on integrating Apache Camel into the project, including configuration and usage.
  • Contributors and Authors: Recognition of the contributors to the project.
  • Contributions: Guidelines on how to contribute to the project, including code standards and pull request requirements, tips and best practices.
  • Code of Conduct: Behavioral guidelines expected for the project community.

5. API and Documentation

6. Endpoints and Database

  • Endpoint Description: Details of the available API endpoints, including methods, parameters, and usage examples.
  • Database Management: Strategies and practices for efficient management of the database used by the project.

7. Testing

  • Testing Strategies: Approach and methods used to test the software, including unit, integration, and E2E tests.
  • Testing Tools: Description of the testing tools used in the project and how to configure them.

8. CI/CD and Automations

  • CI/CD Pipeline: Explanation of the continuous integration and delivery pipeline, detailing each stage and its function.
  • Automations and Artifact Generation: Description of the automations incorporated into the CI/CD, including documentation generation and build artifacts.

9. Configuration Files

10. Best Practices

11. Legal and Licensing

  • Licensing: Information about the rights and restrictions associated with the use of the software.
  • Terms of Use: Information about the terms and conditions for using the software.

12. Projections and Innovations

  • Future Plans: Discussion on functionalities and improvements considered for future versions of the project.
  • Improvement Proposals: Space for the community to suggest and debate improvements and innovations.

13. Attachments and Useful Links

14. Security

  • Security Policy: Details on the supported versions, reporting vulnerabilities, and general security practices.

Clone this wiki locally