# Python Syslog Module

The `syslog` module provides an interface to the Unix `syslog` library routines. This module is intended to be used for logging system messages on Unix systems.

*Note: This module is only available on Unix.*

## Importing the Module

```python
import syslog
```

## Basic Logging

You can log a message using `syslog.syslog()`.

```python
import syslog

syslog.syslog('Processing started')
```

You can also specify the priority level.

```python
syslog.syslog(syslog.LOG_WARNING, 'This is a warning')
syslog.syslog(syslog.LOG_ERR, 'This is an error')
```

## Priorities

The module defines the following priority levels (from highest to lowest):

*   `LOG_EMERG`: System is unusable.
*   `LOG_ALERT`: Action must be taken immediately.
*   `LOG_CRIT`: Critical conditions.
*   `LOG_ERR`: Error conditions.
*   `LOG_WARNING`: Warning conditions.
*   `LOG_NOTICE`: Normal but significant condition.
*   `LOG_INFO`: Informational.
*   `LOG_DEBUG`: Debug-level messages.

## Opening and Closing Logs

You can customize how the log is opened using `openlog()`.

### `syslog.openlog(ident, logoption, facility)`

*   `ident`: A string that is prepended to every message (defaults to `sys.argv[0]`).
*   `logoption`: Bit field options (e.g., `LOG_PID`, `LOG_CONS`).
*   `facility`: The type of program logging the message.

```python
import syslog
import os

syslog.openlog(ident="my_app", logoption=syslog.LOG_PID, facility=syslog.LOG_LOCAL0)
syslog.syslog("Message from my_app")
syslog.closelog()
```

## Setting the Log Mask

You can restrict which priorities are logged using `setlogmask()`.

```python
import syslog

# Only log warnings and errors (and higher)
syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_WARNING))

syslog.syslog(syslog.LOG_WARNING, "This will be logged")
syslog.syslog(syslog.LOG_INFO, "This will NOT be logged")
```

[[programming/python/python]]