Laravel: Adding log channels outside logging.php configuration (plugin development) - php

I'm working on a laravel plugin/package.
We want to define our own loggers outside of the main project configuration (config/logger.php)
I have tried the following in the ServiceProvider register() function.
Based on the MonoLogger test code.
$partLogger = new Logger('vendor_part-log');
$partLogHandler = new StreamHandler(storage_path('logs/vendor/part-log.log', Logger::DEBUG));
$partLogger->pushHandler($partLogHandler);
// MonoLog Registry
Registry::addLogger($partLogger, 'vendor_part-log');
Sadly this doesn't work inside Laravel.
I also can't get the other existing loggers from Registry::
So the problem is that the new channel won't register.
Is there a different Registry in use inside Laravel or do I need an entirely different solution to achieve this?

While we'd still like a more automated solution, we've compromised and are using a static function as source of the configuration.
In config/logging.php:
At the top replace return [... with $config = [....
And at the bottom add the following lines:
$config['channels'] = array_merge(
$config['channels'],
\FooVendor\Bar\Classes\Logs::getLogs(),
);
return $config;
And create the mentioned class with the following function:
/**
* Configs to be merged in config/logging.php
* #return array
*/
public static function getLogs()
{
return [
'foo_partlogger' => [
'driver' => 'single',
'path' => storage_path('logs/foo/partlogger.log'),
'level' => 'debug',
],
'foo_second_partlogger' => [
'driver' => 'single',
'path' => storage_path('logs/foo/second_partlogger.log'),
'level' => 'debug',
],
];
}

Related

Laravel log file specific to a package

I'm writing a couple of laravel packages and I'm wondering if it is possible to have the package write to a specific log file but only for messages related to the package?
I tried making a logging.php file in the packages/myorg/mypackage/config (below) but it doesn't seem to do anything.
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/mypackage.log'),
'level' => env('LOG_LEVEL', 'debug'),
]
]
];
I am using "jeroen-g/laravel-packager" to set up the packages. It appears to manually load the mypackage.config in the ServiceProvider bootForConsole
protected function bootForConsole(): void
{
// Publishing the configuration file.
$this->publishes([
mypackage.'/../config/mypackage.php' => config_path('mypackage.php'),
], 'mypackage.config');
}
I'm not sure how to add custom logging to that though. I'm still learning Laravel and I'm not quite sure what or how the main applications config/logging.php is read so I'm not quite sure how to inject a custom version for an add-on package.
EDIT:
I found a post that suggested using the following in the ServiceManager boot() method:
$this->app->make('config')->set('logging.channels.mychannel', [
/* settings */
]);
I used the package config to set a 'logging' => [ 'channels' => [ 'mychannel' => [ /* settings */ ] ] ] and could then do the same thing as above with:
$this->app->make('config')->set('logging.channels.mychannel', config('mypackage.logging.channels.mychannel');
But that still required something in the code. The next best thing I have found thus far is to change my config/logging.php to config/logging.channels.php and include something like:
return [
'mychannel' => [
'driver' => 'single',
'path' => storage_path('logs/mypackage.log'),
'level' => env('LOG_LEVEL', 'debug'),
]
];
Then in the service provider register() method add:
$this->mergeConfigFrom(__DIR__ . '/../config/logging.channels.php', 'logging.channels');
I tried doing it from the original 'logging.php' with channels array nested in a 'logging' key, but array_merge doesn't appear to merge the nested elements so my channel never showed up in logging.channels.
I'm not sure if this is ideal, however. I'd still like to know if there is a 'better' or best practices way of adding custom package logging parameters and whether there is a need to publish it in any way (and how).

In Laravel can I set a default context for the Log facade

I'm using the Log:: facade a lot and have a helper class called LogHelper which provide me with a static method LogHelper::context() which include many key values I need to track the requests. But having to type it every time for each usage make it error prune and fill not so efficient.
I'm looking for a way to inject the values by default, and allow me to overwrite them if needed specifically.
At the moment this is how I use it,
Log::debug('Request Started', LogHelper::context());
what I'm looking for is to inject the context by default
Log::debug('Request Started');
and have the option to overwrite it, if need it:
Log::debug('Request Started', ['more' => 'context'] + LogHelper::context());
PS, the LogHelper::context() return a simple key => value array which include some staff i need to debug requests, and the reason it do not use the values directly in the message is because i log to graylog as structured data, and this way i can filter by any key.
I have solved this issue by using the tap functionality and $logger->withContext() (note: the latter was added in Laravel 8.49).
You want to create a new class which contains your context logic. I've created an extra Logging folder in app/ in which my logging customizations sit.
app/Logging/WithAuthContext.php:
<?php
namespace App\Logging;
use Illuminate\Log\Logger;
class WithAuthContext
{
public function __invoke(Logger $logger)
{
$logger->withContext([
'ip' => request()?->ip(),
'ua' => request()?->userAgent(),
]);
}
}
Depending on which logging channel(s) you use, you will have to add the class to each one you want to add context to. So in app/config/logging.php:
<?php
use App\Logging\WithAuthContext;
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
// ...
'channels' => [
// ...
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
'tap' => [WithAuthContext::class],
],
// ...
],
];
There is a way, but it is not pretty. You can create a custom monolog logger driver. The process is described at https://laravel.com/docs/8.x/logging#creating-monolog-handler-channels.
Here's a possible implementation:
class ContextEnrichingLogger extends \Monolog\Handler\AbstractHandler {
private $logger;
public function __construct($level = Monolog\Logger::DEBUG, bool $bubble = true, $underlyingLogger = 'single') {
$this->logger = Log::driver($underlyingLogger);
}
public function handle(array $record) {
$record['context'] += LogHelper::context();
return $this->logger->handle($record);
}
}
Then register this as a custom logger in your config/logging.php:
return [
'default' => 'enriched',
//...
'channels' => [
// ...
'enriched' => [
'driver' => 'monolog',
'handler' => ContextEnrichingLogger::class,
'level' => env('APP_LOG_LEVEL', 'debug'),
"with" => [
"underlyingLogger" => env('LOG_CHANNEL', 'single')
]
]
]
];
I haven't tested this particular one but this is how I've defined other custom loggers.
Note, this is probably also achievable via a custom formatter though I think it's probably the same trouble.

Laravel slack log but with additional fields and configuration

In Laravel 5.6 I'm trying to make proper slack logs and I did:
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'slack'],
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'TEST',
'icon' => ':boom:',
'level' => 'info',
],
It works but I want to specify additional fields and maybe customize it a little if it match some other conditions.
I was looking at SlackWebhookHandler.php monolog file but not all parameters work in this configuration..
For example emoji and username doesn't work - I don't know if slack already has even options for changing bot username.
Other example is that in this file something it's called useAttachment and here it's just attachment - where the names are stored..?
Back to topic I did:
Log::info('added test',['test'=>'test']);
And it works, but for slack I want to send additional field, in every request for example:
'added test',['test'=>'test', 'more' => 'test2']
How I'm able to accomplish it? I need to connect to Log Class and slack driver in some way but I don't have idea how to do this?
I debugged myself to SlackRecord::getSlackData, there you see how he handles attachments and add's additional data to the record.
For me it totally fitted to set 'context' => true in logging.php for the Slack Channel and define a Processor which just add's the Data I need to the record
class SlackProcessor {
/**
* #param array $record
* #return array
*/
public function __invoke(array $record) {
$record["context"]["Env"] = env("LOG_SLACK_USERNAME", "localhost");
$record["context"]["Full URL"] = Request::fullUrl();
$record["extra"]["Request Data"] = Request::all();
return $record;
}
}
So maybe you could just debug again to getSlackData and see why he jumps over the attachment part you need.
I was able to get closer to solution but still not at all:
On logging.php now I have
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'tap' => [App\Logging\SlackLogger::class],
'username' => 'BOT',
'attachment' => false,
'emoji' => ':boom:',
'level' => 'info',
],
I created App/Logging/SlackLogger.php:
namespace App\Logging;
use Monolog\Logger;
use Monolog\Handler\SlackWebhookHandler;
use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\JsonFormatter;
class SlackLogger
{
/**
* Customize the given logger instance.
*
* #param \Illuminate\Log\Logger $logger
* #return void
*/
public function __invoke($logger)
{
$dateFormat = "Y-m-d H:i:s";
$checkLocal = env('APP_ENV');
foreach ($logger->getHandlers() as $handler) {
if ($handler instanceof SlackWebhookHandler) {
$output = "[$checkLocal]: %datetime% > %level_name% - %message% `%context% %extra%` :poop: \n";
$formatter = new LineFormatter($output, $dateFormat);
$handler->setFormatter($formatter);
$handler->pushProcessor(function ($record) {
$record['extra']['dummy'] = 'test';
return $record;
});
}
}
}
}
And It works only if I don't try to make custom attachment on slack.. When I'm trying to do:
$handler->pushProcessor(function ($record) {
$record['extra']['dummy'] = 'test';
$record['attachments'] = [
'color' => "#36a64f",
"title" => "Slack API Documentation",
"text" => "Optional text that appears within the attachment"
];
return $record;
});
the $record losts 'attachments' array.. I was checking it in SlackWebhookHandler in write function because at this pushProcessor at return it still exists, but not sending to slack. I know that can be related to $handler->setFormatter($formatter); but I if I remove It, the problem still exists - so I still don't know how to solve it.

Custom (dynamic) log file names with laravel5.6

With laravel 5.5 we had access to configureMonologUsing() method in $app which made it possible for things like this in bootstrap/app.php:
$app->configureMonologUsing(function (Monolog\Logger $monolog) {
$processUser = posix_getpwuid(posix_geteuid());
$processName= $processUser['name'];
$filename = storage_path('logs/laravel-' . php_sapi_name() . '-' . $processName . '.log');
$handler = new Monolog\Handler\RotatingFileHandler($filename);
$monolog->pushHandler($handler);
});
Doing this is useful when your app may be called from different contexts (eg CLI/HTTP) with different users (which is desirable) and file rotation. Doing this prevents write errors in case the log file was created by the HTTP user before the CLI one tries to add something in it and vice-versa.
Handling this is otherwise tricky or insecure as it involves to be able to set write perms on files that may not exist yet.
In addition, it's quite handy to have logs separated by contexts as they usually have little in common and it makes it easier to search among them.
Unfortunately, this way of doing things is not possible anymore with laravel 5.6 and I could not (yet) find a way to transparently do so for all file based logging.
Thanks
Customisation is now done through invoking a custom formatter for Monolog.
Here is an example using daily rotating filenames (as I do).
This can be setup in config/logging.php, note the non-default tap parameter:
'channels' => [
'daily' => [
'driver' => 'daily',
'tap' => [App\Logging\CustomFilenames::class],
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
]
In your custom formatter, you can manipulate the Monolog logger however you wish, similar to configureMonologUsing():
app\Logging\CustomFilenames.php
<?php
namespace App\Logging;
use Monolog\Handler\RotatingFileHandler;
class CustomFilenames
{
/**
* Customize the given logger instance.
*
* #param \Illuminate\Log\Logger $logger
* #return void
*/
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
if ($handler instanceof RotatingFileHandler) {
$sapi = php_sapi_name();
$handler->setFilenameFormat("{filename}-$sapi-{date}", 'Y-m-d');
}
}
}
}
One way to restore your original behaviour is to remove the {date} component from the handler's filenameFormat. A better way might be to manipulate the appropriate handler for the single driver.
See: https://laravel.com/docs/5.6/logging#advanced-monolog-channel-customization
Solution:
step1: create a channel inside the config/logging.php file
example :
'channels' => [
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
'web' => [
'driver' => 'single',
'path' => storage_path('logs/web/web.log'),
],
]
Step2: Now set dyanamic path from controller like this
config(['logging.channels.web.path' => storage_path('logs/web/'.time().'.log')]);
Step3 : now generate your log
Log::channel('web')->info("your message goes here");
Enjoy :)

FastRoute Groups in Zend Expressive

I want use route groups for FastRoute in Expressive.
Like sample:
$router = $app->getContainer()->get(FastRoute\RouteCollector::class);
$router->get('/', App\Action\HomePageAction::class);
$router->addGroup('/pages', function (FastRoute\RouteCollector $router) {
$router->get('', App\Action\PagesIndexAction::class);
$router->get('/add', App\Action\PagesAddAction::class);
$router->get('/edit/{id}', App\Action\PageEditActionFactory::class);
$router->post('/edit/{id}', App\Action\PageEditActionFactory::class);
$router->get('/another/{section}[/{subsection}]', PagesAnotherActionFactory::class);
});
I created factories as written in docs (https://docs.zendframework.com/zend-expressive/features/router/fast-route/#advanced-configuration)
And register their in router.global.php:
// ...
'factories' => [
FastRoute\RouteCollector::class => App\Container\FastRouteCollectorFactory::class,
FastRoute\DispatcherFactory::class => App\Container\FastRouteDispatcherFactory::class,
Zend\Expressive\Router\RouterInterface::class => App\Container\RouterFactory::class,
],
// ...
Now I can not figure out where to write the configuration and how to activate it.
Can this be done in the file config/router.php?
Help me, please.
you can put them in config.router.php as long as the file gets merged with the rest of your config.
'dependencies' => [
//..
'invokables' => [
/* ... */
// Comment out or remove the following line:
// Zend\Expressive\Router\RouterInterface::class => Zend\Expressive\Router\FastRouteRouter::class,
/* ... */
],
'factories' => [
/* ... */
// Add this line; the specified factory now creates the router instance:
FastRoute\RouteCollector::class => App\Container\FastRouteCollectorFactory::class,
FastRoute\DispatcherFactory::class => App\Container\FastRouteDispatcherFactory::class,
// Zend\Expressive\Router\RouterInterface::class => Zend\Expressive\Router\FastRouteRouterFactory::class, // replaced by following line
Zend\Expressive\Router\RouterInterface::class => App\Container\RouterFactory::class,
/* ... */
],
],
Note the dependencies key and that your own RouterFactory replaces the FastRouteRouterFactory because it shares the same config key.
This is not supported and I am not sure if this can be implemented in FastRoute.
You can check the thread "Zend router - child routes"

Categories