Logging Activity

Microsoft Drivers for PHP for SQL Server

Collapse image Expand Image Copy image CopyHover image

By default, errors and warnings that are generated by the Microsoft Drivers for PHP for SQL Server are not logged. This topic discusses how to configure logging activity.

Logging Activity Using the PDO_SQLSRV Driver

The only configuration that is available for the PDO_SQLSRV driver is the pdo_sqlsrv.log_severity entry in the php.ini file.

Add the following at the end of your php.ini file:

  Copy imageCopy Code
[pdo_sqlsrv]
pdo_sqlsrv.log_severity = <num>;

<num> can be one of the following:

Value

Description

0

Logging is disabled.

-1

Specifies that errors, warnings, and notices will be logged.

1

Specifies that errors will be logged.

2

Specifies that warnings will be logged.

4

Specifies that notices will be logged.

Logging information will be added to the phperrors.log file.

Logging Activity Using the SQLSRV Driver

To turn logging on, you can use the sqlsrv_configure function or you can alter the php.ini file. You can log activity on initializations, connections, statements, or error functions. You can also specify whether to log errors, warnings, notices, or all three.

Note Note

You can configure the location of the log file in the php.ini file.

Turning Logging On

You can turn logging on by using the sqlsrv_configure function to specify a value for the LogSubsystems setting. For example, the following line of code configures the driver to log activity on connections:

sqlsrv_configure("LogSubsystems", SQLSRV_LOG_SYSTEM_CONN);

The following table describes the constants that can be used as the value for the LogSubsystems setting:

Value (integer equivalent in parentheses)

Description

SQLSRV_LOG_SYSTEM_ALL (-1)

Turns on logging of all subsystems.

SQLSRV_LOG_SYSTEM_OFF (0)

Turns logging off. This is the default.

SQLSRV_LOG_SYSTEM_INIT (1)

Turns on logging of initialization activity.

SQLSRV_LOG_SYSTEM_CONN (2)

Turns on logging of connection activity.

SQLSRV_LOG_SYSTEM_STMT (4)

Turns on logging of statement activity.

SQLSRV_LOG_SYSTEM_UTIL (8)

Turns on logging of error functions activity (such as handle_error and handle_warning).

You can set more than one value at a time for the LogSubsystems setting by using the logical OR operator (|). For example, the following line of code turns on logging of activity on both connections and statements:

sqlsrv_configure("LogSubsystems", SQLSRV_LOG_SYSTEM_CONN | SQLSRV_LOG_SYSTEM_STMT);

You can also turn logging on by specifying an integer value for the LogSubsystems setting in the php.ini file. For example, adding the following line to the [sqlsrv] section of the php.ini file will turn on logging of connection activity:

sqlsrv.LogSubsystems = 2

By adding integer values together you can specify more than one option at a time. For example, adding the following line to the [sqlsrv] section of the php.ini file will turn on logging of connection and statement activity:

sqlsrv.LogSubsystems = 6

Logging Errors, Warnings, and Notices

After turning logging on, you must specify what to log. You can log one or more of the following: errors, warnings, and notices. For example, the following line of code specifies that only warnings will be logged:

sqlsrv_configure("LogSeverity", SQLSRV_LOG_SEVERITY_WARNING);

Note Note

The default setting for LogSeverity is SQLSRV_LOG_SEVERITY_ERROR. If logging is turned on and no setting for LogSeverity is specified, only errors are logged.

The following table describes the constants that can be used as the value for the LogSeverity setting:

Value (integer equivalent in parentheses)

Description

SQLSRV_LOG_SEVERITY_ALL (-1)

Specifies that errors, warnings, and notices will be logged.

SQLSRV_LOG_SEVERITY_ERROR (1)

Specifies that errors will be logged. This is the default.

SQLSRV_LOG_SEVERITY_WARNING (2)

Specifies that warnings will be logged.

SQLSRV_LOG_SEVERITY_NOTICE (4)

Specifies that notices will be logged.

You can set more than one value at a time for the LogSeverity setting by using the logical OR operator (|). For example, the following line of code specifies that errors and warnings should be logged:

sqlsrv_configure("LogSeverity", SQLSRV_LOG_SEVERITY_ERROR | SQLSRV_LOG_SEVERITY_WARNING);

Note Note

Specifying a value for the LogSeverity setting does not turn logging on. You must turn logging on by specifying a value for the LogSubsystems setting, then specify the severity of what is logged by setting a value for LogSeverity.

You can also specify a setting for the LogSeverity setting by using integer values in the php.ini file. For example, adding the following line to the [sqlsrv] section of the php.ini file enables logging of warnings only:

sqlsrv.LogSeverity = 2

By adding integer values together, you can specify more than one option at a time. For example, adding the following line to the [sqlsrv] section of the php.ini file will enable logging of errors and warnings:

sqlsrv.LogSeverity = 3

See Also