I am having some troubles using the logger service in Symfony2. I find it quite confusing.
I am trying to use a Rotating File Handler. To this handler zou can pass 3 paramenters: the filename of the log, maxFiles that the log can divide itself and log level.
I want to pass this parameters from the controller, since I load that data in runtime from a .ini configuration file, but I am not sure how to do it.
How could I do this?
Try the following in your controller:
add a use statement: use Monolog\Handler\RotatingFileHandler;
use it like this: $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
Related
I want for a certain Laravel Job class to change the behaviour of the logging system. This is how my logs look right now:
[2018-08-22 08:31:24] production.INFO: [do-harvester-job]
[template: 598 - theme: 2592]Doing tasks
This is achieved by the following code:
\Log::info("{$this->harvester_job->log_prefix()}Doing tasks");
The problem is that I have a lot of log calls like this one and it gets cumbersome to always add the call to the log_prefix method.
Is there any way of prepending that info to the log without having to concatenate it inside the log call?
If you are facing problems in displaying the class data inside log file then you can use json_encode($className->fetchAllData()).
I have multiple similar websites that I want to test. Here's the current approach:
Clone an existing codeception project
Adjust the variables
Add some additional specific test cases if required
This works just fine, but has one issue. If the general test case changes that means that I have to go into every project and make the changes. So what I would rather see is a way to include a generic top level test case in every project and call it with a parameter.
I tried so with a simple include() statement, but unfortunately this doesn work. And ideas how I can accomplish this?
Here some code of my initial try:
/home/tests/site1/tests/acceptance/isOnlineCept.php
include("../1generic_tests/isonline.php");
$I = new AcceptanceTester($scenario);
$I->am('user');
$I->wantTo('see the content of the site');
$I->lookForwardTo('see the homepage');
$I->amOnPage('/');
$I->see("Tech, Geek and Rock'n'Roll");
The file I include looks like this
$I = new AcceptanceTester($scenario);
$I->am('user');
$I->wantTo('see the content of the site');
$I->lookForwardTo('see the homepage');
$I->amOnPage('/');
$I->see('something else');
Unfortunately it fails with this error
[PHPUnit_Framework_Exception] Undefined index: class
Maybe include is not the right way to do it, but what would be a better way?
You can use Page Object for this.
Create one Constants.php file using Page Object where you can define all the selectors.
Create one other file where you can keep all common operation going to be performed. E.g. Settings.php file where you can have all methods as per your requirement. you can define it with parameter too.
To access any selector and/or method from declared under page dir can be accessible by using the following line.
use Page\Constants as ConstantsPage;
To access any selector from any specific file
FileNamePage::$selectorName;
To access any method, first create the object and then call the method.
E.g.
$settings = new SettingsPage( $I )
$settings->methodName();
To call the method and/pr selector in the same file.
self::selectorName;
self::methodName();
Hope this will be helpful.
I need write a customlog on laravel 5.3, and modify on runtime for add some extra information.
So far I have managed to do it, but the code does not work because what I can do is add a new instance of the log, which duplicates the lines in each modification of the format.
My handicap is that I do not know how to return or if this is possible, the current handler of the logging system, and simply pass it the format modification.
The code below allows me to modify the format line, but only once.
If I run it again, start duplicating the lines, and what is not like, create the handler in another way, or use the existing one that would be appropriate.
$maxFiles = config('app.log_max_files');
$path = storage_path('/logs/cprsync.log');
// Really I don't need this because this are on Laravel App, but with this method y can create a $handler for modify format
$handler = new RotatingFileHandler($path, is_null($maxFiles) ? 5 : $maxFiles, config('app.log_level'));
(is_null(config('cprsync.activejob'))) ? $job = '' : $job=' ['.config('cprsync.activejob').']';
$logFormat = "[%datetime%] [%level_name%]".$job.": %message% %context% %extra%\n";
$formatter = new LineFormatter($logFormat,null,true,true);
$handler->setFormatter($formatter); // What I really want is to make this change ...
// Push handler
$log->getMonolog()->pushHandler($handler);
I don't know if it's the prettiest (like in Symfony you can just put some YAML rules in the services.yml and you've changed your log format), but this seems to be a very valid option and basically builds on what you're doing already:
http://laravel-tricks.com/tricks/monolog-for-custom-logging
TL;DR: Create a custom logging class with a function write() that basically contains the code OP has posted. Then add that class to your facades and you can log with MyCustomLogger::write($message);
EDIT: This isn't really what OP requested, since this doesn't change the format on runtime.
I am using Laravel Log Facade in my app. And I have several services like Mandrill, Twilio, Stripe, etc., that need to be logged in separate file. But when I use Log::useFiles() to set separate file for one of the service wrapper class, like this:
Class Mailer
{
static function init()
{
Log::useFiles(storage_path('logs/mandrill-'.date("Y-m-d").'.log'));
}
static function send()
{
// some code here...
Log::error("Email not sent");
}
}
And I am ending up with log being written in both Laravel log file, and this Mandrill log file.
Is there a way to tell Log to write logs only in one file?
It's generally strange that it does that, because when I use directly Monolog, it writes only in one file, as it should. As far as I know Log Facade is using Monolog.
First of all, keep in mind that if you change the log handlers in your Mailer class you'll change them for the whole application.
Secondly, the reason that after your changes you get logs written to 2 files is that useFiles() does not overwrite the default log handler but adds a new handler to the handlers that Monolog will use. Therefore, you just add a second handler to the list and both of them handle the log message by saving them into different files.
Thirdly, Laravel's Log facade does not provide a way to replace the default handler - if you want to use it you need to use Monolog directly. You can access it by calling Log::getMonolog().
I have a project based on codeigniter. And I should use one class that extended from a codeigniter controller in another php file. But I didn't find the solution about how to teach another php file to see whole CI-project. Beyond that needed class can not inherit when i call it from other place.
I'm not 100% sure if this helps get you in the right direction, but kudos if it does!
Codeigniter routes the application depending on the environment state of the URI. What you need to do is set the environment and include the index view file like so:
$_SERVER["REQUEST_URI"] = "cms/2";
//Set GET action,method params etc
require_once "path/to/index.php";
When you load CI Index file it reads the SERVER Variable and others which you may have to find and execute the controller and method, I would also advise that you modify the library/view file as it may exit upon output causing your script to exit.
Also you may wis hto look into ob_start() to catch the buffer.