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

  • That's a good thought and maybe one worth proposing via the Support portal.

    If I was to implement this kind of notification I would add a custom value to the config_override, something like

    $sugar_config['logger']['custom_notify'] => 'true',

    then extend the SugarLogger class found in include/SugarLogger.php

    and change the "log" function in that class to include an email to the primary address of the sugar Admin/s if the config value is true.

    Alternatively, and possibly cleaner, instead of adding a config value, add a property to the Admin users (dependent on the User Type == Admin) so that each Admin can choose whether to receive Fatal logger email notifications or not.  

    Then your log function in the SugarLogger class extension would look for users with that setting and email them if the level is Fatal.

    You can leverage the MailerFactory to send the email. 
    You can find an example of a sendEmail function in:

    include/SugarQueue/jobs/AbstractJobNotification.php

     

Comment
  • That's a good thought and maybe one worth proposing via the Support portal.

    If I was to implement this kind of notification I would add a custom value to the config_override, something like

    $sugar_config['logger']['custom_notify'] => 'true',

    then extend the SugarLogger class found in include/SugarLogger.php

    and change the "log" function in that class to include an email to the primary address of the sugar Admin/s if the config value is true.

    Alternatively, and possibly cleaner, instead of adding a config value, add a property to the Admin users (dependent on the User Type == Admin) so that each Admin can choose whether to receive Fatal logger email notifications or not.  

    Then your log function in the SugarLogger class extension would look for users with that setting and email them if the level is Fatal.

    You can leverage the MailerFactory to send the email. 
    You can find an example of a sendEmail function in:

    include/SugarQueue/jobs/AbstractJobNotification.php

     

Children
No Data