Avoid overloading sugarcrm.log by channelling logs

Hello Sugar community,

Just came across the following that I would like to share. 

Leveraging PSR-3 Logger we can configure log channels for our specific Sugar customisations. 

As an example, let’s imagine I need to implement a custom component that I would like to log separately into separate file.

I’ll start by adding the following entries on my config_override.php file.

 

$sugar_config['logger']['channels']['custom_job_log']['level'] = 'debug';
$sugar_config['logger']['channels']['custom_job_log']['handlers'][0]['type'] = 'File';
$sugar_config['logger']['channels']['custom_job_log']['handlers'][0]['level'] = 'info';
$sugar_config['logger']['channels']['custom_job_log']['handlers'][0]['name'] = 'custom/custom_job';
$sugar_config['logger']['channels']['custom_job_log']['handlers'][0]['logSize'] = '10MB';
$sugar_config['logger']['channels']['custom_job_log']['handlers'][0]['maxLogs'] = 10;
$sugar_config['logger']['channels']['custom_job_log']['handlers'][0]['suffix'] = '%m-%Y';

 After having that, on the custom scheduler I just need to add the following:

    //Scheduler Custom Logger entry
    $CustomJobLogger = Factory::getLogger('custom_job_log');
    $CustomJobLogger->error("Error in our new custom log");
  

As the new log file location is inside the custom/ folder I can download it with the Diagnostic Tool.

This can help us separating the logs but also allows increasing the log level of our custom channel maintaining sugarcrm.log as fatal. 

I hope this helps and happy logging.


Thanks for the hints

André

Parents
  • Hi ,

    thank you for this usefull solution.

    I have a related question : what is the best solution to be warned when a fatal error happened (instead of having to check the logs every days etc.) ? Of course we can imagine using a log checker solution, but it's not compliant with several of our customer expectation, nor with cloud solution.

    So would it be to customized the core logger to, for instance, send an email when a fatal error happened ? Or log an activity in Sugar ?

    Any idea is welcome.

    Fred

  • Hello 

    That's a great point, I do not have experience with this myself to say with certainty what would be the best solution. 
    If you leverage a custom logger you can send the SugarCRM log entries to a log management application and handle the alerts there, I believe that would be the most solid approach. 

    If you want to keep it in Sugar, with a custom logger you can also store the fatal entries in a custom Sugar module.
    After having the entries there I would use a scheduler job to send an alert gathering a batch of records each time.

    What do you think? Did you come up with different ideas yourself? 

Comment Children
No Data