Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Logs with username


Go to End


6 Posts   1232 Views

Avatar
swaiba

Forum Moderator, 1899 Posts

6 July 2011 at 5:05am

Hi,

I am using the...

SS_Log::add_writer(new SS_LogEmailWriter('myemail@somedomain.com', SS_Log::ERR);

and I get emails for my errors sent to me - however what would be pure gold is if I could also get the name of the user (assuming someone is logged in) in that email so I know who to ask for when I want more information about what they were doing... any advice is much appreciated.

Barry

Avatar
martimiz

Forum Moderator, 1391 Posts

7 July 2011 at 8:10am

I think maybe something along the line of (see the documentation in the header of class SS_Log):

$logEmailWriter = new SS_LogEmailWriter('my@email.com');
$myEmailFormatter = new MyLogEmailFormatter();
$logEmailWriter->setFormatter($myEmailFormatter);

SS_Log::add_writer($logEmailWriter, SS_Log::ERR);

Have MyLogEmailFormatter extend the SS_LogErrorEmailFormatter class. Then have it add Member::currentUser() somewhere?

No idea if this will work though, haven't tried :-)

Avatar
swaiba

Forum Moderator, 1899 Posts

7 July 2011 at 8:20pm

I was hoping it was a setting - but that is very close I should have followed the code a little more... I am going to set this up now and I'll report back what has worked.

Avatar
martimiz

Forum Moderator, 1391 Posts

7 July 2011 at 10:03pm

Edited: 07/07/2011 10:04pm

Maybe I've overlooked a setting - don't think so though... Very interested in what you'll come up with :-)

Avatar
swaiba

Forum Moderator, 1899 Posts

7 July 2011 at 10:42pm

Edited: 07/07/2011 10:46pm

All working....

_config.php

	$logEmailWriter = new SS_LogEmailWriter('some@email.com');
	$myEmailFormatter = new MyLogErrorEmailFormatter();
	$logEmailWriter->setFormatter($myEmailFormatter);
	SS_Log::add_writer($logEmailWriter, SS_Log::ERR);

MyLogErrorEmailFormatter.php

class MyLogErrorEmailFormatter extends SS_LogErrorEmailFormatter {
	public function format($event) {
		$arr = parent::format($event);

		$member=Member::currentUser();
		$arr['data'] .= 'Member='.$member->ID.','.$member->FirstName.','.$member->Surname."\r\n\r\n";

		$browser = new Browser();
		$arr['data'] .= $browser."\r\n\r\n";

		if (count($_POST)) {
			ob_start();
			print_r($_POST);
			$arr['data'] .= ob_get_contents();
			ob_end_clean();
		}

		if (count($_GET)) {
			ob_start();
			print_r($_GET);
			$arr['data'] .= ob_get_contents();
			ob_end_clean();
		}

		return $arr;
	}
}

As you can see I've also included the get/post and browser in this too...
http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/

Avatar
martimiz

Forum Moderator, 1391 Posts

8 July 2011 at 12:01am

Nice! Together we're strong :-)