Skip to main content

syslog-rsyslog-intro

原文地址

About

The Syslog module logs events by sending messages to the logging facility of your web server's operating system. Syslog is an operating system administrative logging tool that provides valuable information for use in system management and security auditing. Most suited to medium and large sites, Syslog provides filtering tools that allow messages to be routed by type and severity.

It is not suitable for shared hosting environments.

It might not be as user friendly as Database Logging but will allow you to see logs and troubleshoot if your site is not accessible. Because the Database logging module writes logs to the database, it can slow down the website. By using Syslog you can improve the performance of the site.

syslog or rsyslog

Recent versions of Debian and CentOS use rsyslog instead of syslog.

Using Syslog

Here is a four-step guide to switching from Drupal's default database. It is for Linux systems only.

1. Install & configure the Syslog module

  • Enable the module Syslog on the Modules page at admin/modules
  • Configure the module on the 'Logging and errors' configuration page at admin/config/development/logging
  • Select which Syslog facility to attach to the log messages. Choose one that is not in use by Syslog.
  • Set the syslog identity (a string that will be prepended to every message logged to Syslog), for example drupal_www_example_com.

2. Configure Syslog to log to a separate file (optional)

You can write the log messages to a separate file (If you don't the messages will most likely end up in /var/log/messages)

Edit the appropriate syslog configuration file. To find this file, choose one of the following three options:

  • Option 1: Newer operating systems with override (1)

    /etc/rsyslog.d/drupal.conf

    or

  • Option 2: Newer operating systems without override

    /etc/rsyslog.conf

    or

  • Option 3: Older operating systems without override

    /etc/syslog.conf

Note: (1) The Linux community consensus is to always override Linux core configuration files, such as, but not limited to, the rsyslog.conf file. Instead of hacking Linux core. Some of the benefits with overriding is that future Linux updates are much easier, faster, and are less risky to result in your configuration being deleted. The Drupal community's Coding Standards has the same consensus with Drupal core. If the rsyslog.d folder and drupal.conf file do not exist, you simply need to create them with appropriate permissions on each. The drupal.conf file name could be any name to your liking. This Linux override standard is supported by newer Linux operating systems who support rsyslog.

Add the following line at the bottom of the configuration file:

local0.\* /var/log/drupal.log

Where local0 is the Syslog facility that you chose in the Syslog configuration (see Step 1)

Restart the syslog daemon. To do so, choose one of the following four options. Which one option you choose depends on the operating system and your preferred command:

  • Option 1: Linux systemctl rsyslog

    sudo systemctl restart rsyslog.service

    or

  • Option 2: Linux service rsyslog

    sudo service rsyslog restart

    or

  • Option 3: Linux service syslog

    sudo service syslog restart

    or

  • Option 4: Mac OS X launchctl syslogd

    sudo launchctl unload /System/Library/LaunchDaemons/com.apple.syslogd.plist
    sudo launchctl load /System/Library/LaunchDaemons/com.apple.syslogd.plist

Check that you are seeing messages in the syslog:

tail /var/log/drupal.log

This shows the bottom of the log file. To see the complete log file use:

less /var/log/drupal.log

To see new log messages appearing in the log file use:

tail -f /var/log/drupal.log

Exit using ctrl+c

3. Disable the Database Logging (formerly, Watchdog) module (optional)

If you have the Database logging (dblog) enabled, disable it at on the Modules page /admin/build/modules.

4. Configure Syslog to log to a separate file for each domain

You can use separate log files for each website.

Edit the syslog configuration file at:

/etc/syslog.conf

or

/etc/rsyslog.conf

and add:

Note that the following syntax uses an if block (RainerScript) and is only available in rsyslog, not syslog.

if $programname == 'drupal\_www\_example\_com' then /var/log/drupal/drupal\_www\_example\_com.log
& stop

local0.\* /var/log/drupal/others.log
& stop

$programname drupal_www_example_com is the Sylog identity that you have set in the Logging and errors configuration page, (admin/config/development/logging), see step 1.

With the last line you are sure to put all Drupal log (if you choose facility local0) in one file.

The "strange" lines, & ~, omit to put the log record in other files (for example /var/log/syslog)

Note If you use rsyslog with a /etc/rsyslog.d/ folder, you can create a new file there, with the same content (preferred method).

More information

Write drupal logs to rsyslog instead of to dblog

See also the PHP's documentation pages for the openlog and syslog functions.

On syslog facilities, severity levels, and how to setup a syslog.conf file, see UNIX/Linux syslog.conf.

Notes

On Windows Server 2003R2, syslog doesn't write successfully to the Event Log. Instead, an Application Warning appears (Source: PHP-5.2.0) which starts with:"The description for Event ID ( 3 ) in Source ( PHP-5.2.9 ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer". According to http://www.php.net/manual/en/function.syslog.php#41832 , the default IUSR_ needs to be added to the Users group

php.ini needs to explicitly state that error logs are written to syslog (ie. error_log = syslog), otherwise the syslog module will not work.