1

I am running Centos/Apache with PHP 5.5.14, and am using syslog to write to a log file as follows:

syslog(LOG_INFO,'Some message');

The logs are being written to /var/log/messages and look like Aug 10 15:48:16 devserver httpd: Some message, however, the log file is also cluttered with a bunch of logs that look like Aug 10 15:48:21 devserver kernel: usb 1-1.2: USB disconnect, device number 83.

How do make PHP sent logs to its own dedicated log file?

3
  • What do you mean by $_SERVER['Some message']? The $_SERVER superglobal uses uppercase key names to represent data such as environment and HTTP request headers, so it's a bit confusing when you try to lookup Some message as a key. Commented Aug 10, 2014 at 23:53
  • @Jailout2000. You are correct. That was a typo. Original post fixed. Commented Aug 11, 2014 at 0:41
  • It might be worth looking into using MonoLog to handle your logging needs, it makes setting up logging a bit less painful and also offers all kinds of logging options Commented Mar 10, 2017 at 9:36

2 Answers 2

3

Before calling the function syslog, you should call openlog and specify the “syslog tag”. With this framework, the messages are tagged and you can use the system logger (rsyslog, syslog-ng, etc.) to send these messages to a specific log file, send them to a specific log server, send an email for emergency messages, etc.

This blog post “Using syslog for your php applications” gives more details on how to setup the whole thing.

Sign up to request clarification or add additional context in comments.

Comments

0

Syslog dumps data into a singular log file. A lot of system services, even the kernel as you pointed out, dumps to syslog, and by extension, the same log file.

If you want to take advantage of your own personal log file, you'll need to use filesystem IO functions.

Example:

<?php

function writeLog($message) {
  $handle = fopen('/var/log/project1/myproject1.log', 'a');
  $line   = '[' . date('Y-m-d H:i:s T') . '] ' . $message . "\n";
  fwrite($handle, $line);
  fclose($handle);
}

You should take care to make sure you don't fopen() and then subsequently fclose() a lot, since that will severely slow down your code. You should only fopen() when you need to write/read, and then leave the file handle open until your PHP code exits (or let PHP do garbage collection for you).

You'll also need to make sure your web server has sufficient privileges to write to the /var/log/project1/ directory, or at the very least, append/write access to the filename specified. This includes making sure SELinux is happy if you have it enabled.

As a bonus, you could throw your custom log file into logrotate daemon's config and have your logs automatically rotated, compressed, archived, and whatnot.

2 Comments

This will have terrible performance as you're opening and closing a file with every call. I know you stated it in your answer, but there are far better approaches to logging than this.
@GordonM I'd expect more advanced applications to send logs over the network rather than to disk every call, such as to a zeromq or otherwise asynchronous I/O server, to be consumed and stored to a more permanent storage later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.