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

Log File Name Pattern #758

Open
r-anwar opened this issue Jan 29, 2025 · 7 comments
Open

Log File Name Pattern #758

r-anwar opened this issue Jan 29, 2025 · 7 comments
Labels
question Further information is requested

Comments

@r-anwar
Copy link

r-anwar commented Jan 29, 2025

Support Request

Is it possible to add the Servers Hostname to the filename? Like $HOSTNAME_blitz-2025-01-27.log

We would provide the hostname through the .env file.

Plugin Version

4.23.7

@r-anwar r-anwar added the question Further information is requested label Jan 29, 2025
@bencroker
Copy link
Collaborator

Not currently, and I don’t think Craft allows this either. You can create your own logger to catch specific logs and format them however you like using Monolog. I have an article that should help you get started:
https://putyourlightson.com/articles/adding-logging-to-craft-plugins-with-monolog

@r-anwar
Copy link
Author

r-anwar commented Jan 29, 2025

Hi Ben, I got following answer from craft support:

I just had a quick test and I was able to rename the web log for Craft via the config/app.php file:
$currentHost = $_SERVER['HTTP_HOST'] ?? 'localhost';

return [
'components' => [
'log' => [
'monologTargetConfig' => [
'name' => $currentHost .'-web',
],
],
],
];
As far as I know, queue and console are no domain specific so those would be tricky.
For Blitz, again, I'm not sure how loggin is done there, but It might be as straightforward as adding a config/app.blitz.php file with similar code to above.
I hope that helps!

We require the hostname in the log file name because our setup consists of multiple nodes that share a storage folder. This storage folder is an NFS mount that is used by all nodes

@bencroker
Copy link
Collaborator

bencroker commented Jan 29, 2025

It’s not possible to modify the Blitz log target via config, but you can add your own using a custom plugin/module.

if (Craft::getLogger()->dispatcher instanceof Dispatcher) {
    Craft::getLogger()->dispatcher->targets[] = new MonologTarget([
        'name' => ($_SERVER['HTTP_HOST'] ?? 'localhost') . '-blitz',
        'categories' => ['blitz'],
        'level' => LogLevel::INFO,
        'logContext' => false,
        'allowLineBreaks' => false,
        'formatter' => new LineFormatter(
            format: "[%datetime%] %message%\n",
            dateFormat: 'Y-m-d H:i:s',
        ),
    ]);
}

This is basically a copy of the registerLogTarget method.
https://github.com/putyourlightson/craft-blitz/blob/18893251941da7d75db573560caa6c9af2d028fb/src/Blitz.php#L291C22-L306

Let me know if this helps.

@r-anwar
Copy link
Author

r-anwar commented Jan 29, 2025

Would that replace the default blitz log file?

@bencroker
Copy link
Collaborator

bencroker commented Jan 29, 2025

No, that would add a new logger. You could remove the existing Blitz logger using something like this.

if (Craft::getLogger()->dispatcher instanceof Dispatcher) {
    foreach (Craft::getLogger()->dispatcher->targets as $key => $target) {
        if ($target instanceof MonologTarget && $target->getName() === 'blitz') {
            unset(Craft::getLogger()->dispatcher->targets[$key]);
        }
    }

    // Add a custom Blitz logger
}

@r-anwar
Copy link
Author

r-anwar commented Jan 30, 2025

Hi Ben, is there an event I can hook on to unset the blitz logger? Because my module's code is executed before Blitz is initialized, or rather, the existing logger targets are web, console, & queue at the runtime of my module, so at the time there is no blitz logger present.

@bencroker
Copy link
Collaborator

bencroker commented Jan 30, 2025

Wrapping your code in this should work.

Craft::$app->onInit(function() {

});

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

No branches or pull requests

2 participants