0

We are building a web application with CWP 1.8 which is upon SilverStripe 3.6, the log will be captured by SS_Log class with statement like SS_Log::log('Log message', SS_Log::INFO);. As we only need to capture the log for debugging purpose when the error happens, so we would like to make the logger more flexible that it can be controlled with a variable in the config file. In other words, we need to turn on the logging when the error produced and turn off the logging once the error sorted.

Can anyone give us some directions to achieve that? Thanks so much

SilverStripe 3.6

scrowler
  • 24,273
  • 9
  • 60
  • 92
  • "In other words, we need to turn on the logging when the error produced and turn off the logging once the error sorted" - is this in a specific part of your code, or whenever you find an error in your app you'd turn it on for the whole app then turn it off again? Note that CWP expects to report logs to the system so it can be tracked in your Graylog instance – scrowler Feb 13 '19 at 04:50
  • Hi Robbie, thanks for the feedback. It's not part of the code at the moment but we are wondering if we can achieve that in some way. The expected behaviour is we can change a config variable in one of the config file to turn on/off the SS_Log because we only want to capture the log when we facing any issue that reported by the user. – user9585476 Feb 13 '19 at 22:15

1 Answers1

1

TL;DR: Use CWP's Graylog - everything is tracked in there already.


Logging has various levels:

class SS_Log
{
    const ERR = Zend_Log::ERR;
    const WARN = Zend_Log::WARN;
    const NOTICE = Zend_Log::NOTICE;
    const INFO = Zend_Log::INFO;
    const DEBUG = Zend_Log::DEBUG;

These map to a few of the Zend Log constants.

With CWP you get some default logging out of the box:

// Tee logs to syslog to make sure we capture everything regardless of project-specific SS_Log configuration.
SS_Log::add_writer(new \SilverStripe\Auditor\MonologSysLogWriter(), SS_Log::DEBUG, '<=');

This means that everything from debug level will be sent to the syslog by default. This is so the CWP platform can pipe your website logs into Graylog (the centralised logging system), which you can access with your CWP Dash account.

If you need extra logging on top of this, you can configure it in one of two ways:

  1. define('SS_ERROR_LOG', '/path/to/logfile.log'): This will be automatically configured to write from warning levels up into your log file. This isn't particularly useful to you in the context of CWP because you don't have access to the server to check the files.
  2. Add your own custom logger and configure it to send logs wherever you want (the example in these docs is an email address).

If you want to enable or disable this with a variable, you only really have the option of adding or removing custom code, since CWP doesn't give you the ability to customise environment variables. You'd need to do a deployment in order to change this code, so you may as well add something like this to mysite/_config.php and comment it out when you don't need it:

// Log everything and send it to my email address for debugging. Comment out when
// not required.
SS_Log::add_writer(new SS_LogEmailWriter('your-email@example.com'), SS_Log::DEBUG, '<=');

Again, you'll need to do a deployment whenever you change this code though.


Theory aside, my recommendation would be to use the logs that are already available in Graylog.

scrowler
  • 24,273
  • 9
  • 60
  • 92
  • Thank you very much Robbie, it's very helpful. And I got another question in terms of the message logged in Graylog, we are using something like SS_Log::log("CUSTOM_MESSAGE", SS_Log::INFO); my question is what is the length of the `CUSTOM_MESSAGE` and is it configurable? Thanks – user9585476 Feb 26 '19 at 22:51
  • I don't understand, sorry. That part is dynamic - it can be whatever you want it to be (and as long as you want it to be) – scrowler Feb 26 '19 at 22:58
  • Sorry for the confusion ... The issue is when we send some long message to graylog, the message will be truncated. – user9585476 Feb 26 '19 at 23:06
  • Sounds like this is a Graylog/Elasticsearch limitation. You can't configure CWP Graylog at all, so you might want to use an email logger or something else that won't truncate long messages - or split the message up if possible into multiple chunks. – scrowler Feb 27 '19 at 00:10