Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add logger service #24

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

feat: add logger service #24

wants to merge 3 commits into from

Conversation

Hitesh-Kandpal
Copy link
Contributor

@Hitesh-Kandpal Hitesh-Kandpal commented Aug 26, 2024

Changes in PR:

  1. Add logger service

Summary by CodeRabbit

  • New Features
    • Introduced a centralized logging service that enhances the application's logging capabilities with structured and flexible logging options.
    • Added enumerations for log levels and severity, standardizing logging practices across the application.
    • Added various logging methods for different severity levels: info, warn, error, and fatal, enabling improved monitoring and debugging.
    • Each log entry is formatted consistently, making it easier to track and analyze logs associated with user sessions.

Copy link

coderabbitai bot commented Aug 26, 2024

Walkthrough

A new LoggerService class has been introduced in the Angular application, providing structured logging capabilities. This service includes enumerations for logging levels and severity, along with methods for logging at various severity levels. The class formats log messages into a JSON structure and maintains a session identifier for associating logs with user sessions, enhancing the application's logging mechanism.

Changes

Files Change Summary
src/app/core/enums/log-level.enum.ts Added LogLevel enum with members: INFO, WARN, ERROR, FATAL; Added Severity enum with members: LOW, MEDIUM, HIGH.
src/app/core/services/logger.service.ts Added LoggerService class with methods: log, info, warn, error, and fatal for structured logging.

Poem

In the code where bunnies hop,
A logger now can never stop!
With levels high and levels low,
It captures all, from fast to slow.
So let us cheer with whiskers bright,
For logs that shine both day and night! 🐇✨


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 57bad28 and 9ef947c.

Files selected for processing (2)
  • src/app/core/enums/log-level.enum.ts (1 hunks)
  • src/app/core/services/logger.service.ts (1 hunks)
Additional comments not posted (9)
src/app/core/enums/log-level.enum.ts (1)

1-12: LGTM!

The LogLevel and Severity enums are well-defined and follow best practices:

  • The enums provide a standardized way to categorize log messages and issues based on their severity and impact.
  • The naming convention is followed, and the enums are exported correctly.
  • The enum members are defined using string values, which maintains readability and avoids magic numbers.

These enums will help in maintaining consistent logging and effective issue handling across the application.

src/app/core/services/logger.service.ts (8)

15-17: LGTM!

The method correctly generates a unique session identifier using the current timestamp.


19-44: LGTM!

The method correctly formats the log message into a JSON structure, including all the relevant log information.


46-72: LGTM!

The method correctly logs messages at various severity levels, providing default values for optional parameters and formatting the log message using the formatLog method.


75-77: LGTM!

The method correctly retrieves the current screen name using window.location.pathname and handles the case when the pathname is not available by returning a default value.


80-82: LGTM!

The method correctly returns the source of the log. Consider making the source value configurable in the future if needed.


85-112: LGTM!

The method correctly retrieves device information using the user agent string and window properties, handles various platforms, and returns the device information as a JSON string for easy inclusion in the log message.


115-117: LGTM!

The method correctly generates a unique traceback identifier using the current timestamp and a random string.


119-140: LGTM!

The info, warn, error, and fatal methods correctly log messages at their respective severity levels by calling the log method with the appropriate log level and passing all the required parameters for proper logging.

Also applies to: 142-163, 165-186, 188-209


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai generate interesting stats about this repository and render them as a table.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (7)
src/app/core/services/logger.service.ts (7)

1-1: Consider adding a brief class-level comment.

Adding a comment at the beginning of the class to describe its purpose and usage can improve readability and maintainability.

+/**
+ * LoggerService provides structured logging capabilities.
+ * It includes methods for logging at various severity levels and formats log messages into a JSON structure.
+ */

11-36: Add comments to describe the parameters and the return value.

Adding comments to describe the parameters and the return value can improve readability and maintainability.

+/**
+ * Formats log messages into a JSON structure.
+ * @param {string} level - The log level (e.g., INFO, WARN, ERROR, FATAL).
+ * @param {string} severity - The severity of the log message.
+ * @param {string} tracebackId - The traceback identifier.
+ * @param {string} screenName - The name of the screen where the log originated.
+ * @param {string} source - The source of the log message.
+ * @param {string} deviceInfo - Information about the device.
+ * @param {string} data - Additional data to log.
+ * @param {string} message - The log message.
+ * @param {string} [stackTrace=''] - The stack trace (optional).
+ * @param {string} [timestamp=''] - The timestamp (optional).
+ * @returns {string} The formatted log message as a JSON string.
+ */

38-65: Add comments to describe the parameters.

Adding comments to describe the parameters can improve readability and maintainability.

+/**
+ * Logs messages at various severity levels.
+ * @param {string} level - The log level (e.g., INFO, WARN, ERROR, FATAL).
+ * @param {string} severity - The severity of the log message.
+ * @param {string} tracebackId - The traceback identifier.
+ * @param {string} screenName - The name of the screen where the log originated.
+ * @param {string} source - The source of the log message.
+ * @param {string} deviceInfo - Information about the device.
+ * @param {string} data - Additional data to log.
+ * @param {string} message - The log message.
+ * @param {string} [stackTrace] - The stack trace (optional).
+ * @param {string} [timestamp] - The timestamp (optional).
+ */

67-89: Add comments to describe the parameters.

Adding comments to describe the parameters can improve readability and maintainability.

+/**
+ * Logs messages at the INFO level.
+ * @param {string} severity - The severity of the log message.
+ * @param {string} tracebackId - The traceback identifier.
+ * @param {string} screenName - The name of the screen where the log originated.
+ * @param {string} source - The source of the log message.
+ * @param {string} deviceInfo - Information about the device.
+ * @param {string} data - Additional data to log.
+ * @param {string} message - The log message.
+ * @param {string} [stackTrace] - The stack trace (optional).
+ */

91-112: Add comments to describe the parameters.

Adding comments to describe the parameters can improve readability and maintainability.

+/**
+ * Logs messages at the WARN level.
+ * @param {string} severity - The severity of the log message.
+ * @param {string} tracebackId - The traceback identifier.
+ * @param {string} screenName - The name of the screen where the log originated.
+ * @param {string} source - The source of the log message.
+ * @param {string} deviceInfo - Information about the device.
+ * @param {string} data - Additional data to log.
+ * @param {string} message - The log message.
+ * @param {string} [stackTrace] - The stack trace (optional).
+ */

114-135: Add comments to describe the parameters.

Adding comments to describe the parameters can improve readability and maintainability.

+/**
+ * Logs messages at the ERROR level.
+ * @param {string} severity - The severity of the log message.
+ * @param {string} tracebackId - The traceback identifier.
+ * @param {string} screenName - The name of the screen where the log originated.
+ * @param {string} source - The source of the log message.
+ * @param {string} deviceInfo - Information about the device.
+ * @param {string} data - Additional data to log.
+ * @param {string} message - The log message.
+ * @param {string} [stackTrace] - The stack trace (optional).
+ */

137-158: Add comments to describe the parameters.

Adding comments to describe the parameters can improve readability and maintainability.

+/**
+ * Logs messages at the FATAL level.
+ * @param {string} severity - The severity of the log message.
+ * @param {string} tracebackId - The traceback identifier.
+ * @param {string} screenName - The name of the screen where the log originated.
+ * @param {string} source - The source of the log message.
+ * @param {string} deviceInfo - Information about the device.
+ * @param {string} data - Additional data to log.
+ * @param {string} message - The log message.
+ * @param {string} [stackTrace] - The stack trace (optional).
+ */
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 10ffe80 and 57bad28.

Files selected for processing (1)
  • src/app/core/services/logger.service.ts (1 hunks)

providedIn: 'root',
})
export class LoggerService {
private sessionId = 'sample-session-id'; // Replace with actual session ID logic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a method here to generate a session Id too.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a method for generating session id

stackTrace?: string,
): void {
this.log(
'WARN',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the values like log level, severity should come from enum values.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added enum values for log level and severity and used them in logger service

level: string,
severity: string,
tracebackId: string,
screenName: string,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THis should handle filling up most generic fields like screenName, deviceInfo, source, timestamp etc.
When in use we only add message and stacktrace. Also what is the plan/idea for tracebackId?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrote function for filling these generic fields

The tracebackId will be a unique identifier for each log entry, helping us trace and correlate related logs. It's generated using a combination of the current timestamp and a random value to ensure uniqueness.
(I saw this was added in initial setup for pickleball logger service file, so I added it here too)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants