How to use SS_log and log - example code anywhere?

Silverstripe Version: 3.1


I’m new to SilverStripe and php but have to maintain an existing code base.
My first goal is to understand the existing code.
I’ve tried to add logging using below code but get errors.
Being overwhelmed by the sheer amount of new things to learn I’m looking for a code snippet to learn from or a pointer to a relevant document
Note: the API manual for V3 API doc wasn’t really helpful for me as it contains no working code example

	SS_Log::add_writer(new SS_LogFileWriter('data/www/mysite/assets/Uploads/my.log'), SS_Log::ERR);
		 $msg =  "Start";
		 echo $msg;
		 log($msg, SS_Log::NOTICE);
gives run time error:
[Warning] log() expects parameter 1 to be double, string given

You’re not far off. The issue is that you’re using the wrong log() method. On it’s own like that, you’re calling the standard PHP method - this is an arithmetical function (PHP: log - Manual) which is why you’re seeing the error about expecting a numeric input.

You need to call log on the logging class, so it would be something like:

SS_Log::log($msg, SS_Log::NOTICE);

The other thing to be aware of is that you’re designating the writer for Errors, and are throwing a notice so it may not get added to your log. You’ll either want to change one of the priorities, or you can add a comparison to the writer so that, for example, it logs everything from Error and down

1 Like

Thank you so much, this got me over the hurdle :slight_smile: