{"id":19357754,"url":"https://github.com/joomla-framework/log","last_synced_at":"2025-04-23T11:30:36.741Z","repository":{"id":7094541,"uuid":"8385825","full_name":"joomla-framework/log","owner":"joomla-framework","description":"[DEPRECATED] Joomla Framework Log Package","archived":false,"fork":false,"pushed_at":"2021-09-18T11:01:27.000Z","size":112,"stargazers_count":4,"open_issues_count":1,"forks_count":5,"subscribers_count":13,"default_branch":"1.x-dev","last_synced_at":"2024-06-21T05:03:52.092Z","etag":null,"topics":["joomla","joomla-framework","logging","php","psr-3"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joomla-framework.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"joomla","custom":"https://community.joomla.org/sponsorship-campaigns.html"}},"created_at":"2013-02-24T03:25:26.000Z","updated_at":"2022-12-16T22:02:20.000Z","dependencies_parsed_at":"2022-08-19T17:31:17.097Z","dependency_job_id":null,"html_url":"https://github.com/joomla-framework/log","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Flog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Flog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Flog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Flog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joomla-framework","download_url":"https://codeload.github.com/joomla-framework/log/tar.gz/refs/heads/1.x-dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223922117,"owners_count":17225636,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["joomla","joomla-framework","logging","php","psr-3"],"created_at":"2024-11-10T07:09:04.894Z","updated_at":"2024-11-10T07:09:05.633Z","avatar_url":"https://github.com/joomla-framework.png","language":"PHP","funding_links":["https://github.com/sponsors/joomla","https://community.joomla.org/sponsorship-campaigns.html"],"categories":[],"sub_categories":[],"readme":"## The Log Package [![Build Status](https://ci.joomla.org/api/badges/joomla-framework/log/status.svg)](https://ci.joomla.org/joomla-framework/log)\n\n[![Latest Stable Version](https://poser.pugx.org/joomla/log/v/stable)](https://packagist.org/packages/joomla/log) [![Total Downloads](https://poser.pugx.org/joomla/log/downloads)](https://packagist.org/packages/joomla/log) [![Latest Unstable Version](https://poser.pugx.org/joomla/log/v/unstable)](https://packagist.org/packages/joomla/log) [![License](https://poser.pugx.org/joomla/log/license)](https://packagist.org/packages/joomla/log)\n\n### Deprecated\n\nThe `joomla/log` package has been deprecated.  No further updates are planned.\n\n### Introduction\n\nThe Joomla Framework includes a Log package that allows for configurable, hook-driven logging to a variety of formats.\n\nThe classes included in the Log package are `Joomla\\Log\\Log`, `Joomla\\Log\\LogEntry`, `Joomla\\Log\\AbstractLogger` as well as the classes `Joomla\\Log\\Logger\\Callback`, `Joomla\\Log\\Logger\\Database`, `Joomla\\Log\\Logger\\Echoo`, `Joomla\\Log\\Logger\\FormattedText`, `Joomla\\Log\\Logger\\Syslog` and `Joomla\\Log\\Logger\\W3c` which support formatting and storage. Of all these classes, you will generally only use `Joomla\\Log\\Log` in your projects.\n\nLogging is a two-step process.\n\nFirst you must add the add loggers to listen for log messages. Any number of loggers can be configured to listen for log messages based on a priority and a category. For example, you can configure all log messages to be logged to the database, but also set just errors to be logged to a file. To do this, you use the `Joomla\\Log\\Log::addLogger` method.\n\nAfter at least one logger is configured, you can then add messages using the `Joomla\\Log\\Log::addLogEntry` method where you can specify a message, and optionally a priority (integer), category (string) and date.\n\n### Logging priority\n\nBefore we look at any logging examples, we need to understand what the priority is. The priority is an integer mask and is set using one or more predefined constants in the `Joomla\\Log\\Log` class. These are:\n\n* Joomla\\Log\\Log::EMERGENCY\n* Joomla\\Log\\Log::ALERT\n* Joomla\\Log\\Log::CRITICAL\n* Joomla\\Log\\Log::ERROR\n* Joomla\\Log\\Log::WARNING\n* Joomla\\Log\\Log::NOTICE\n* Joomla\\Log\\Log::INFO\n* Joomla\\Log\\Log::DEBUG\n\nFor information on what situation to use each constant in see the PSR-3 (Section 3) details here - https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#3-psrlogloggerinterface where a detailed explanation can be found.\n\nA final constant, `Joomla\\Log\\Log::ALL` is also available which corresponds to hex FFFF (16 bits). The other constants reserve the first eight bits for system use. This allows the developer the last eight bits, hex 100 to 8000, for custom use if desired. As the values are for masking, they can be mixed using any of the bitwise operators for *and*, *or*, *not* and *xor*.\n\nBy default, loggers are added to listen for `Joomla\\Log\\Log::ALL` priorities and log entries are added using the `Joomla\\Log\\Log::INFO` mask.\n\n### Logging to files *(formattedtext)*\n\nA very typical example of logging is the ability to log to a file, and this is the default handler for logging. To do this add the logger and then you can add log messages.\n\n```php\n// Initialise a basic logger with no options (once only).\nJoomla\\Log\\Log::addLogger(array());\n\n// Add a message.\nJoomla\\Log\\Log::add('Logged');\n```\n\nAs no logger has been specified in the `Joomla\\Log\\Log::addLogger` call, the \"formattedtext\" logger will be used. This will log the message to a file called \"error.php\" in the log folder specified by the \"log_path\" configuration variable (in the Joomla CMS, the default is `/logs/`). It will look something like this:\n\n    #\u003c?php die('Forbidden.'); ?\u003e\n    #Date: 2011-06-17 02:56:21 UTC\n    #Software: Joomla Framework 01-Jun-2011 06:00 GMT\n\n    #Fields: datetime   priority    category    message\n    2011-06-17T03:06:44+00:00   INFO    -   Logged\n\nThe file is tab-delimited and the default columns are the timestamp, the text representation of the priority, the category and finally the message. The category is empty (a dash) because we didn't supply it.\n\nTo log a different priority, you can use code like:\n\n```php\nJoomla\\Log\\Log::add('Logged 3', Joomla\\Log\\Log::WARNING, 'Test');\n```\n\nThe log file will now look similar to the following:\n\n    2011-06-17T03:06:44+00:00 INFO - Logged\n    2011-06-17T03:52:08+00:00 WARNING - Logged 2\n    2011-06-17T03:57:03+00:00 WARNING test Logged 3\n\n#### Additional options with formattedtext\n\nWhen adding the \"formattedtext\" logger, the following options are available to supply in the array you pass to `Joomla\\Log\\Log::addLogger`.\n\nOption              | Description\n------------------- | ----------------\ntext\\_file          | Allows you to specify the name of the file to which messages are logged.\ntext\\_file\\_path    | Allows you to specify the folder path to the file to which messages are logged.\ntext\\_file\\_no\\_php | If set, the PHP die statement will not be added to the file line of the file.\ntext\\_entry\\_format | Allows you to change the format of the entire line of the log message in the file.\n\n### Changing the name of the log file\n\nGiven the options outlined in the previous section, you can change the name of the file to which you are logging when you add the logger, like this:\n\n```php\n// Log to a specific text file.\nJoomla\\Log\\Log::addLogger(\n\tarray(\n\t\t'text_file' =\u003e 'mylogs.php'\n\t)\n);\n```\n\n#### Logging different priorities to different files\n\nYou can log different types of messages to different files by adding multiple loggers that bind different log priorities to different files. For example, the following code will log all messages except errors to one file, and error messages to a separate file.\n\n```php\n// Log all message except errors to mylogs.php.\nJoomla\\Log\\Log::addLogger(\n\tarray(\n\t\t'text_file' =\u003e 'mylogs.php'\n\t),\n\tJoomla\\Log\\Log::ALL ^ Joomla\\Log\\Log::ERROR\n);\n\n// Log errors to myerrors.php.\nJoomla\\Log\\Log::addLogger(\n\tarray(\n\t\t'text_file' =\u003e 'myerrors.php'\n\t),\n\tJoomla\\Log\\Log::ERROR\n);\n```\n\n#### Logging specific categories to a file\n\nIf you are wanting to collect errors for your specific project, class or extension, you can also bind logging to different categories. For example, the following code could be used in a Joomla extension to just collect errors relating to it.\n\n```php\n// Log my extension errors only.\nJoomla\\Log\\Log::addLogger(\n\tarray(\n\t\t'text_file' =\u003e 'com_hello.errors.php'\n\t),\n\tJoomla\\Log\\Log::ERROR,\n\t'com_hello'\n);\n```\n\nTo log messages to that logger, you would use something similar to the following code:\n\n```php\nJoomla\\Log\\Log::add('Forgot to say goodbye', Joomla\\Log\\Log::ERROR, 'com_hello');\n```\n\nIt is important to note that other loggers, added beyond your control, may also pick up this message.\n\n#### Splitting up logs by date\n\nLog files can, potentially, get very long over time. A convenient solution to this is to roll logs into different files based on a period of time - an hour, a day, a month or even a year. To do this, you just need to add the date to the file name of the log file. The following example shows you how to do this on a daily basis.\n\n```php\n// Get the date.\n$date = date('Y-m-d');\n\n// Add the logger.\nJoomla\\Log\\Log::addLogger(\n\tarray(\n\t\t'text_file' =\u003e 'com_hello.'.$date.'.php'\n\t)\n);\n```\n\n#### Changing the format of the log message\n\nWhen you adding a log message, it is written to the file in a default format in the form:\n\n    {DATETIME} {PRIORITY} {CATEGORY} {MESSAGE}\n\nEach field is written in upper case, wrapped in curly braces and separated by tabs. There are a number of other fields that are automatically defined in the \"formattedtext\" logger that you can take advantage of automatically. These are:\n\nField      | Description\n---------- | -----------\n{CLIENTIP} | The IP address of the user.\n{DATE}     | The \"Y-m-d\" date component of the message datestamp.\n{TIME}     | The \"H:i:s\" time component of the message datestamp.\n\nTo modify for the log format to add any or all of these fields, you can add the logger as shown in the following code.\n\n```php\n// Add the logger.\nJoomla\\Log\\Log::addLogger(\n\tarray(\n\t\t'text_file' =\u003e 'com_hello.php',\n\t\t'text_entry_format' =\u003e '{DATE} {TIME} {CLIENTIP} {CATEGORY} {MESSAGE}'\n\t)\n);\n```\n\nAs you can see, you can include or leave out any fields as you require to suit the needs of your project.\n\nYou can also add more fields but to do this you need to create and add a `Joomla\\Log\\LogEntry` object directly. The following example shows you how to do this.\n\n```php\n// Add the logger.\nJoomla\\Log\\Log::addLogger(\n\tarray(\n\t\t'text_file' =\u003e 'com_shop.sales.php',\n\t\t'text_entry_format' =\u003e '{DATETIME} {PRICE} {QUANTITY} {MESSAGE}'\n\t),\n\tJoomla\\Log\\Log::INFO,\n\t'Shop'\n);\n\n$logEntry = new Joomla\\Log\\LogEntry('T- Shirt', Joomla\\Log\\Log::INFO, 'Shop');\n$logEntry-\u003eprice = '7.99';\n$logEntry-\u003equantity = 10;\n\nJoomla\\Log\\Log::add($logEntry);\n```\n\nIt is strongly recommended that, when using a custom format, you bind the log entries to a specific and unique category, otherwise log files with different format *(fields)* could become mixed.\n\n### Logging to the database\n\nThe \"database\" logger allows you to log message to a database table. The create syntax for the default table is as follows:\n\n```sql\nCREATE TABLE `jos_log_entries` (\n\t`priority` int(11) DEFAULT NULL,\n\t`message` varchar(512) DEFAULT NULL,\n\t`date` datetime DEFAULT NULL,\n\t`category` varchar(255) DEFAULT NULL,\n\tKEY `idx_category_date_priority` (`category`,`date`,`priority`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n```\n\nTo log messages using the \"database\" logger, you the following code as a guide.\n\n```php\n// Add the logger.\nJoomla\\Log\\Log::addLogger(\n\tarray(\n\t\t'logger' =\u003e 'database'\n\t),\n\tJoomla\\Log\\Log::ALL,\n\t'dblog'\n);\n\n// Add the message.\nJoomla\\Log\\Log::add('Database log', Joomla\\Log\\Log::INFO, 'dblog');\n```\n\nNotice that the example binds the logger to all message priorities, but only those with a category of \"dblog\".\n\nIf you are wanting to store additional information in the message, you can do so using a JSON encoded string. For example:\n\n```php\n// Assemble the log message. Assume a $user object.\n$log = array(\n\t'userId' =\u003e $user-\u003eget('id'),\n\t'userName' =\u003e $user-\u003eget('name'),\n\t'stockId' =\u003e 'SKU123',\n\t'price' =\u003e '7.49',\n\t'quantity' =\u003e 10\n);\n\n// Add the message.\nJoomla\\Log\\Log::add(json_encode($log), Joomla\\Log\\Log::INFO, 'dblog');\n```\n\nThis makes it possible to retrieve detailed information for display.\n\n\n## Installation via Composer\n\nAdd `\"joomla/log\": \"~1.0\"` to the require block in your composer.json and then run `composer install`.\n\n```json\n{\n\t\"require\": {\n\t\t\"joomla/log\": \"~1.0\"\n\t}\n}\n```\n\nAlternatively, you can simply run the following from the command line:\n\n```sh\ncomposer require joomla/log \"~1.0\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoomla-framework%2Flog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoomla-framework%2Flog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoomla-framework%2Flog/lists"}