JLog - How do I add anonymized client IPs to my log files? Topic is solved

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
gba
Joomla! Intern
Joomla! Intern
Posts: 67
Joined: Tue Jun 03, 2014 3:37 pm

JLog - How do I add anonymized client IPs to my log files?

Post by gba » Wed May 01, 2019 3:34 pm

Hello!

Using the JLog class I want to log also the IP addresses of the users.
According to the new GDPR without consent this is allowed, if the IP address is anonymized.

My code:

Code: Select all

JLog::addLogger(
	array(
		'text_file' => 'mylog.log.php',
		'text_entry_format' => '{DATE}|{TIME}|{CLIENTIP}|{PRIORITY}|{MESSAGE}'
	),
	JLog::ALL,
	array('mylog')
);
How can I make JLog::add() replacing the last two or three digits of the IP with asterisks?
Thank you very much in advance for any useful hint!

Kind regards,
Gerald
Last edited by toivo on Fri May 03, 2019 8:24 am, edited 1 time in total.
Reason: mod note: moved from 3.x Extensions

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 11216
Joined: Thu Feb 15, 2007 5:48 am
Location: Suzhou, China

Re: JLog - How do I add anonymized client IPs to my log files?

Post by toivo » Sat May 04, 2019 6:08 am

You could do that in your own extension through the callback parameter in the options of the JLog::addLogger function, where you register your own logger, a cloned version of libraries/src/Log/Logger/FormattedtextLogger.php. The formatLine() function grabs the IP address and returns the log entry.

It may be possible just to extend the FormattedtextLogger class and override the formatLine() function. Not tested.
Toivo Talikka, Global Moderator

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1599
Joined: Fri Jul 05, 2013 10:35 am
Location: Unknown

Re: JLog - How do I add anonymized client IPs to my log files?

Post by SharkyKZ » Sat May 04, 2019 9:24 am

You can set client IP after creating LogEntry object.

Code: Select all

use Joomla\CMS\Log\LogEntry;
use Joomla\Utilities\IpHelper;

// Get IP.
$ip = IpHelper::getIp();

// Anonymize $ip here any way you want.

// Create log entry and set IP.
$entry = new LogEntry($message, $priority, $category);
$entry->clientIP = $ip;

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 11216
Joined: Thu Feb 15, 2007 5:48 am
Location: Suzhou, China

Re: JLog - How do I add anonymized client IPs to my log files?

Post by toivo » Sat May 04, 2019 9:53 am

Thanks, that looks brilliant :)
Toivo Talikka, Global Moderator

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 25957
Joined: Mon Oct 27, 2008 9:27 pm
Location: Akershus, Norway

Re: JLog - How do I add anonymized client IPs to my log files?

Post by Per Yngve Berg » Sat May 04, 2019 1:01 pm

Code: Select all

$ip = preg_replace('/(\d{1,3}\.)(\d{1,3}\.)(\d{1,3}\.)(\d{1,3})\', '$1*\.$3$4', IpHelper::getIp());
Will replace 2. octet with a "*".

gba
Joomla! Intern
Joomla! Intern
Posts: 67
Joined: Tue Jun 03, 2014 3:37 pm

Re: JLog - How do I add anonymized client IPs to my log files?

Post by gba » Wed May 08, 2019 1:17 pm

SharkyKZ wrote:
Sat May 04, 2019 9:24 am
You can set client IP after creating LogEntry object.

Code: Select all

use Joomla\CMS\Log\LogEntry;
use Joomla\Utilities\IpHelper;

// Get IP.
$ip = IpHelper::getIp();

// Anonymize $ip here any way you want.

// Create log entry and set IP.
$entry = new LogEntry($message, $priority, $category);
$entry->clientIP = $ip;
Hi!

Using your code and

Code: Select all

JLog::add($entry);
I achieved what I needed.
Thank you for your hint!

Kind regards,
Gerald


Post Reply

Return to “Joomla! 3.x Coding”