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

Setup winston logger #15

Merged
merged 2 commits into from
Sep 28, 2024
Merged

Setup winston logger #15

merged 2 commits into from
Sep 28, 2024

Conversation

likui628
Copy link
Owner

@likui628 likui628 commented Sep 28, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a new logging system using the Winston library, enhancing logging capabilities with daily log rotation.
    • Added new environment variables for logging configuration, improving customization options for users.
  • Improvements

    • Updated the application to use the new logging system for startup messages, providing clearer and more structured logs.

Copy link

coderabbitai bot commented Sep 28, 2024

Walkthrough

The pull request enhances the application's logging system by incorporating the winston logging library along with its daily rotation file transport. A new configuration file, logger.ts, is created to manage log levels and formats based on the environment. Additionally, the application's startup logging mechanism is updated to utilize the new logger instead of console.log, ensuring a more structured and consistent approach to log messages.

Changes

File Change Summary
package.json Added dependencies: winston at version ^3.14.2 and winston-daily-rotate-file at version ^5.0.0.
.env.sample Introduced new environment variables for logging configuration: LOG_LEVEL, LOG_FILE_PATH, LOG_DATE_PATTERN, LOG_ZIPPED_ARCHIVE, LOG_MAX_SIZE, LOG_MAX_FILES.
src/config/logger.ts Introduced a new logger configuration using winston, configured for different environments, with daily log file rotation and specific logging formats.
src/index.ts Replaced console.log with logger.info for logging the application start message, changing the message format from "Listening: http://localhost:${port}" to "Listening to port ${port}".

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant Logger
    App->>Logger: Initialize logger
    Logger-->>App: Logger ready
    App->>Logger: Log application start
    Logger-->>App: Log message output
Loading

Poem

In the burrow where logs now flow,
A new friend, Winston, starts to glow.
With daily files that spin and twirl,
Our messages dance, a joyful swirl.
Hopping high, we cheer and play,
Logging brightens up our day! 🐇✨


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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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: 2

🧹 Outside diff range and nitpick comments (3)
src/index.ts (1)

7-7: Good replacement of console.log with structured logging.

The use of logger.info instead of console.log is a significant improvement. It provides better structure and consistency in logging, allowing for easier log management and potential integration with log analysis tools.

A minor suggestion: Consider including the server's base URL in the log message for completeness.

You might want to include the full URL in the log message. Here's a suggested change:

-  logger.info(`Listening to port ${port}`)
+  logger.info(`Server is running at http://localhost:${port}`)

This provides more context in the logs, which can be helpful for debugging or when running on different environments.

src/config/logger.ts (1)

12-21: LGTM: Well-structured logger configuration with a minor suggestion.

The logger configuration is well done, considering different environments and using both file and console transports.

Consider adding a timestamp to the log format for better traceability:

winston.format.printf(
  ({ level, message, timestamp }) => `${timestamp} ${level}: ${message}`
),

You can add the timestamp by including winston.format.timestamp() in the format.combine() array.

package.json (1)

35-36: LGTM! Consider adding a script for log rotation.

The addition of winston and winston-daily-rotate-file dependencies is appropriate for setting up a winston logger with daily log rotation. The versions are up-to-date and the use of caret ranges allows for minor version updates, which is a good practice.

Consider adding a npm script in the "scripts" section to manage log rotation or cleanup, such as:

"scripts": {
  ...
  "rotate-logs": "node scripts/rotateLogs.js"
}

This would make it easier to manage log files, especially in a production environment.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between fc71a8e and fad47ce.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (3)
  • package.json (1 hunks)
  • src/config/logger.ts (1 hunks)
  • src/index.ts (1 hunks)
🔇 Additional comments (4)
src/index.ts (1)

3-3: LGTM: Proper import of the new logger.

The import of the logger from the ./config/logger file aligns well with the PR objective of setting up the winston logger. This change centralizes the logging configuration and allows for consistent logging across the application.

src/config/logger.ts (3)

1-2: LGTM: Imports are correct and necessary.

The imports for Winston and its daily rotate file transport are appropriate for setting up the logger.


12-12: LGTM: Logger export is correct.

The logger is correctly exported, allowing it to be imported and used in other parts of the application.


1-22: Great implementation of Winston logger with room for minor enhancements.

Overall, this is a solid implementation of a logging system using Winston. It considers different environments, uses both file and console logging, and sets up a daily rotate file system for log management.

To further improve this implementation, consider:

  1. Making file transport configuration more flexible through environment variables.
  2. Adding error handling for the file transport.
  3. Including a timestamp in the log format.

These enhancements will make the logging system more robust and adaptable to different deployment scenarios.

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 and nitpick comments (1)
.env.sample (1)

7-13: LGTM: Logging variables added, with minor improvement suggestions.

The addition of Winston logging configuration variables is a good step towards improving the application's logging capabilities. However, consider the following suggestions to enhance usability:

  1. Provide example values or brief descriptions for each variable to guide users. For instance:

    LOG_LEVEL=info
    LOG_FILE_PATH=logs/%DATE%.log
    LOG_DATE_PATTERN=YYYY-MM-DD
    LOG_ZIPPED_ARCHIVE=true
    LOG_MAX_SIZE=20m
    LOG_MAX_FILES=14d
    
  2. Expand the comment to briefly explain the purpose of these variables:

    # Winston logger configuration (https://github.com/winstonjs/winston#readme)
    # These variables control log levels, file rotation, and retention policies
    

These changes would make it easier for developers to understand and configure the logging system correctly.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between fad47ce and 4382843.

📒 Files selected for processing (2)
  • .env.sample (1 hunks)
  • src/config/logger.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/config/logger.ts
🔇 Additional comments (2)
.env.sample (2)

Line range hint 1-5: LGTM: Existing environment variables are well-defined.

The existing environment variables, including NODE_ENV, ACCESS_TOKEN_SECRET, REFRESH_TOKEN_SECRET, and DATABASE_URL, are correctly defined and follow common practices for environment configuration.


Line range hint 1-13: Overall: Good addition of logging configuration, enhancing application observability.

The changes to .env.sample successfully introduce Winston logging configuration variables without disrupting existing settings. This addition will enable more flexible and powerful logging capabilities in the application, improving observability and debugging.

To further enhance the usability of this sample file, consider implementing the suggestions provided in the previous comment regarding example values and expanded comments.

@likui628 likui628 merged commit 249a172 into main Sep 28, 2024
3 checks passed
@likui628 likui628 deleted the logger branch September 28, 2024 08:08
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.

1 participant