Download the PHP package tribeos/http-logger without Composer

On this page you can find all versions of the php package tribeos/http-logger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package http-logger

PHP HTTP Logger

A simple HTTP request, response and error logger for API projects in PHP. It can be used with different types of middleware (depending on the framework or library), as well as pure PHP projects.

Getting started

Prerequisites

This guides assumes that you are currently using (or planning to use) the following technologies in your project:

Installation

  1. First, make sure that you have installed and configured a running web server (such as Apache) with PHP 7.1 or higher.
  2. Use composer to install the library: composer require tribeos/http-log

Basic usage

Before using the library, you first have to require the generated autoload.php file:

After that, you can use the HttpLog\* namespace and functions you need in your project.

In order to start logging HTTP requests and responses, you need to first create an HttpLogger object. The object constructor has three required parameters, and an optional fourth one:

  1. type: Type of logger (for now, only the file logger is supported; SQL and MongoDB loggers are planned for addition at a latter date; default: file)
  2. filter: Log filter. It represents which request/response properties will be logged (default: full+h = all supported properties + request and response headers) (see more)
    • features several default filter configurations, as well as the option to define custom filters
  3. path: Path to the log file (default: logs/debug.log)
    • Make sure that your target folder exists and that you have the correct write permissions.
    • Note: Any custom path can be specifed, but it has to be specified as an absolute path (e.g. by using __DIR__) to the project root. The reason for this is that certain PHP functions which can be triggered by the library change the execution path, so we must rely on absolute paths.
  4. default_log (optional): Controls whether the errors will also be logged to the default Apache/PHP log file (default: true)
    • The library logs all errors to its specified path file by design. With the default_log parameter, you can also let all errors be logged to the default error log file, in addition to the library's log file.
    • Note: Fatal PHP errors (which are irrecoverable) are always logged to the default error log file, regardless of this parameter.

Example logger initialization looks like this:

HttpLogger::create() constructs a static instance of the logger (which can later be reused anywhere in the code), while HttpLogger::get() returns the newly created logger instance.

These methods can be chained, as seen in the example above, or called individually, like this:

After logger initialization, the logger is ready to be used. Depending on your project needs and technologies used, call the log() method either at the end of your response outputs, or in specific middlewares that are set to fire after an API response has been sent.

Log format

The log is formatted as a TSV textual file, with all logged parameters separated by tabs (\t), which allows for easy parsing and formatting. Each request/response "pair" is represented by a single line in the file, starting with the request time, and followed by request data, response data and any encountered errors.

Example (sent with Postman):

Logged parameters

The following parameters are available for logging (and used in the default configuration):

Filters

The library allows for different logging filters to be applied to the logger. The filters (specified as the second parameter in the logger creation) specify which of the aforementioned parameters will be logged (and in which order).

Each built-in filter features two variants: the base variant and the headers variant. The base variant includes all parameters related to that filter, except full header information (request_headers and response_headers are absent). The headers variant contains both the parameters and all header information.

The available (base variant) filters are:

Each of these filters (except error) also features a headers variant (standard+h, full+h, request_only+h, response_only+h), which includes the additional header information (request headers after request properties and response headers after response properties).

The full+h filter is set as the default in the library, meaning that the logger will store all request and response data, along with request and response headers.

Custom filters

The library allows for the creation of custom filters, allowing the users to set the specific properties they want to log, as well as their order.

The custom filters are defined as follows: property1|property2|property3|... (properties separated by a pipe symbol - |).

When parsed by the logger, only the properties specified in the custom filter will be logged, disregarding the rest.

Example:

The logger in this example will only log the request date, requested URL, request method and the client's IP address, in that exact order.

Error logging

In addition to request and response logging, the library also features error intercepting and handling. The libary is able to log and gracefully handle PHP notices, warnings, errors and fatal errors. The errors will be logged on the same line after request and response data, as encoded JSON strings.

Example:

How are errors handled?

During the request/response lifecycle, the PHP script is left to execute. The logger is setting one of its methods as the error handler (via set_error_handler()), and intercepting any irregular flows.

All encountered errors are stored in the logger's error stack (array), and after the log() method is called, all errors are logged to the same line as the request and response data, allowing for an easy review of irregular flows that may have occured during execution.

Notices and warnings will be stored in the error stack, and the regular script execution will continue as normal, until the logging is performed and the script finishes execution naturally. However, serious errors (user errors and fatal PHP errors) will, after being logged, stop the script's execution at the time of occurence and return a formatted JSON response to the user, with the details of the fatal error.

The error object contains the following parameters (all of which are logged):

The error filter

Some users might only care about the encountered errors, and not necessarily about request and response data. For that reason, it is possible to apply the error filter during logger initialization.

With the error filter applied, only the errors will be logged, in TSV format (as opposed to formatted JSON in all other filters). Moreover, each error will be logged on a separate line, and include a date property.

Resulting log:

User-defined errors

Apart from automatically managing encountered errors, the library also provides users with the ability to log a custom error message anywhere in the project (similar to how Log4j works).

The library contains the following logging methods, which can be called from the logger object:

Example:

These user-defined errors "behave" in the same way as regular errors: debug messages, warnings and info messages are simply logged, whereas errors and fatal errors also stop program execution. Moreover, the logger will have no difficulties logging both the regular errors and the user-defined errors in a single session.

If the default_log parameter in the logger initialization is set to true, user-defined errors will also be logged in the default Apache/PHP log file.

Resulting log:

Additional information and notes

Library documentation

Extensive documentation can be found at: https://aldin-sxr.github.io/http-logger/

The documentation was generated with PHPDocumentor.

Authors

Acknowledgements

License

The skeleton is licensed under the MIT license. See the LICENSE file for details.


Work in progress by tribeOS - The Fairest, Most Profitable Advertising Marketplace Ever.


All versions of http-logger with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package tribeos/http-logger contains the following files

Loading the files please wait ....