Jump to content

Is it possible to use HTML tags in log files and how?


Juergen
 Share

Recommended Posts

Hello @ all,

I am logging all submissions via my contact form in the log files with the following line of code:

 

$log->save('contact-form', 'Email: '.$emailvalue.'<br />Betreff: '.$subjectvalue.'<br /> Nachricht: '.$commentsvalue);

 

So the logfile is called "contact-form". Unfortnunately all br-tags will be displayed as letters and not as line-breaks. Maybe someone can give me a hint how to use line-breaks in log files.

Thanks

Link to comment
Share on other sites

I don't think this is possible because WireLog replaces the escape characters with spaces:

// somewhere in WireLog::save()
$text = str_replace(array("\r", "\n", "\t"), ' ', $text);

and it also sanitizes the tags when outputting.

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

Its a fast and simply way to store sent mails. Its only there for the purpose if someone says he has sent a mail, but the site owner doesnt receive it. The log file will be written after PHP mailers send function was successfully triggered.

Link to comment
Share on other sites

You won't be able to use $log->save() for the reason @alxndre mentioned, and you won't be able to use the admin log viewer because a) it uses new lines to identify each log entry so therefore one entry cannot contain more than one line, and b) it entity encodes HTML for security reasons.

But you can use PHP functions like error_log() or file_put_contents() to create your own log file (create the "custom-logs" folder first).

$text = date("Y-m-d H:i:s") . "\nHello, how \nare you?\n\n"; // your text, include newlines if needed
error_log($text, 3, $config->paths->assets . "custom-logs/mail.txt")

 

  • Like 6
Link to comment
Share on other sites

3 minutes ago, Robin S said:

But you can use PHP functions like error_log() or file_put_contents() to create your own log file (create the "custom-logs" folder first).

Or you can steal one or two methods @ryan's code, stuff them into your little module, tweak a few lines and add even more fun to PW ;)

Attached is a quick & dirty little module (actually, two of them) that adds:

  • $log->mailLog($your, $fields, $ending, $with, ..., $messagebody) [pass as many arguments as you like; the last one will always have white-space set to wrap]
  • a special log viewer at Setup -> Mail Log that displays as many columns as you sent to mailLog()

It uses some unlikely to naturally occur text patterns for newlines and field separators and fools ProcessLogger::executeView into thinking it is pointed to the mail log, and that way lets it do the heavy lifting.

ProcessMailLogger.zip

5abd6a3d36e4a_PWMailLogs.thumb.jpg.b4fea2bc8d214cadcb6a8e751d72fe9b.jpg

  • Like 10
Link to comment
Share on other sites

I had this problem once and solved it with JSON. It is quick and simple.

In your case

$logmsg = array(
	'Email' => $emailvalue, 
	'Betreff' => $subjectvalue, 
	'Nachricht' => $commentsvalue
);
$log->save('contact-form', json_encode($logmsg));

 

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...