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(logging): reopen on sighup #7140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

james-d-elliott
Copy link
Member

@james-d-elliott james-d-elliott commented Apr 11, 2024

This implements a method to send Authelia a SIGHUP signal to indicate it should reload the configuration file. This can be used in two ways. If using an external logrotate service you can tell Authelia to reopen the file and create it if it doesn't exist. Secondly if you use the existing time date replacements this will create a brand new log file with the current time.

Closes #4964

Summary by CodeRabbit

  • New Features
    • Introduced a new Signal Service for enhanced signal handling capabilities.
    • Added new logging functionalities, including file log management and dynamic log file reopening.
  • Enhancements
    • Improved command context with added methods for accessing logger, providers, and configuration.
  • Refactor
    • Major updates in service management to utilize a new context interface for better modularity and maintenance.
  • Tests
    • Updated logging tests to reflect new functionalities and removed outdated tests.

@authelia
Copy link

authelia bot commented Apr 11, 2024

Artifacts

These changes are published for testing on Buildkite, DockerHub and GitHub Container Registry.

Docker Container

  • docker pull authelia/authelia:feat-logrotate
  • docker pull ghcr.io/authelia/authelia:feat-logrotate

Copy link
Contributor

coderabbitai bot commented Apr 11, 2024

Walkthrough

The update focuses on enhancing the logging mechanism for a service by introducing a new SignalService for signal handling, refactoring existing logging functionalities, and integrating methods to reopen log files post-rotation. These changes aim to improve log management practices, especially in conjunction with tools like logrotate.

Changes

Files Change Summary
internal/commands/const.go Added a new constant serviceTypeSignal.
internal/commands/context.go Added methods to access logger, providers, and configuration in CmdCtx struct.
internal/commands/services.go Added NewSignalService, ServiceCtx interface, updated functions to use ServiceCtx.
internal/logging/const.go Declared global variables and added necessary imports.
internal/logging/file.go Introduced a package for logging to a file with various file handling functionalities.
internal/logging/logger.go Refactored logger initialization, file handling, and added functions for log file reopening.
internal/logging/logger_test.go Updated test imports, removed and retained specific test functions.
internal/logging/printf.go Added new functions to create instances of specific loggers based on log level.
internal/logging/util.go Introduced a new file providing a function for formatting file paths with timestamps.
internal/logging/util_test.go Added tests for the FormatFilePath function in the logging package.

Assessment against linked issues

Objective Addressed Explanation
Reopen logfile after rotation (#4964)
Support create option in logrotate (#4964)
Handle -HUP/-USR1/-USR2 signals for log rotation (#4964) The changes mention improvements in signal handling but lack specific details on handling -HUP/-USR1/-USR2 signals explicitly.

Recent Review Details

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 926d2b2 and 47d049a.
Files selected for processing (6)
  • internal/commands/const.go (1 hunks)
  • internal/commands/context.go (1 hunks)
  • internal/commands/services.go (7 hunks)
  • internal/logging/const.go (1 hunks)
  • internal/logging/file.go (1 hunks)
  • internal/logging/logger.go (3 hunks)
Files skipped from review as they are similar to previous changes (5)
  • internal/commands/const.go
  • internal/commands/context.go
  • internal/logging/const.go
  • internal/logging/file.go
  • internal/logging/logger.go
Additional comments not posted (3)
internal/commands/services.go (3)

89-97: LGTM! The NewSignalService function is well-implemented and follows good practices in logging and parameter handling.


270-323: LGTM! The SignalService struct and its methods are correctly implemented to handle signals and perform actions. Good use of logging and error handling.


367-375: LGTM! The svcSignalLogReOpenFunc function correctly handles the creation of a SignalService for log file reopening based on the configuration. Proper use of conditional checks and service creation.


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>.
    • 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 show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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
Contributor

@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: 1

internal/logging/logger_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@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: 1

internal/logging/logger.go Show resolved Hide resolved
Copy link

codecov bot commented Apr 12, 2024

Codecov Report

Attention: Patch coverage is 47.41379% with 61 lines in your changes are missing coverage. Please review.

Project coverage is 73.57%. Comparing base (cd32d5c) to head (a77b0fa).
Report is 3 commits behind head on master.

❗ Current head a77b0fa differs from pull request most recent head 47d049a. Consider uploading reports for the commit 47d049a to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #7140      +/-   ##
==========================================
- Coverage   73.68%   73.57%   -0.12%     
==========================================
  Files         346      347       +1     
  Lines       29973    30052      +79     
  Branches      839      839              
==========================================
+ Hits        22087    22110      +23     
- Misses       7015     7070      +55     
- Partials      871      872       +1     
Flag Coverage Δ
backend 74.45% <47.41%> (-0.13%) ⬇️
frontend 65.77% <ø> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
internal/commands/context.go 77.57% <100.00%> (+0.50%) ⬆️
internal/logging/const.go 100.00% <ø> (ø)
internal/logging/logger.go 92.42% <28.57%> (-7.58%) ⬇️
internal/logging/file.go 69.69% <69.69%> (ø)
internal/commands/services.go 40.44% <34.28%> (-4.20%) ⬇️

... and 11 files with indirect coverage changes

Copy link
Contributor

@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

@@ -59,9 +59,9 @@ func ConfigureLogger(config schema.Log, log bool) (err error) {

switch {
case config.FilePath != "":
var file *os.File
file = NewFile(config.FilePath)
Copy link
Contributor

Choose a reason for hiding this comment

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

Declare the file variable before using it to avoid scope issues.

+ var file *FileHandler
  file = NewFile(config.FilePath)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
file = NewFile(config.FilePath)
var file *FileHandler
file = NewFile(config.FilePath)

Comment on lines +89 to +96
// Reopen handles safely reopening the log file.
func Reopen() (err error) {
if file == nil {
return fmt.Errorf("error reopening log file: file is not configured")
}

return file.Open()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider encapsulating the file variable within a struct to manage its scope and lifecycle more effectively.

type Logger struct {
    file *FileHandler
}

func (l *Logger) Reopen() error {
    if l.file == nil {
        return fmt.Errorf("error reopening log file: file is not configured")
    }
    return l.file.Open()
}

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// Reopen handles safely reopening the log file.
func Reopen() (err error) {
if file == nil {
return fmt.Errorf("error reopening log file: file is not configured")
}
return file.Open()
}
type Logger struct {
file *FileHandler
}
func (l *Logger) Reopen() error {
if l.file == nil {
return fmt.Errorf("error reopening log file: file is not configured")
}
return l.file.Open()
}

Signed-off-by: James Elliott <james-d-elliott@users.noreply.github.com>
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.

A way to tell authelia to reopen its logfile
1 participant