Php log file in ubuntu

Where are the Apache and PHP log files?

I’ve installed Apache, PHP, and MySQL on Ubuntu 10.10 desktop edition, and it’s working fine.
Except I have no clue where to look for Apache or PHP log files.

5 Answers 5

By default, /var/log/apache2/error.log .

This can be configured in /etc/php5/apache2/php.ini .

Check these settings in php.ini :

  1. error_reporting = E_ALL | E_STRICT (as recommended for development in php.ini)
  2. error_log = /var/log/php_errors.log
  3. Then create log file manually
touch /var/log/php_errors.log chown www-data: /var/log/php_errors.log chmod +rw /var/log/php_errors.log 
sudo service apache2 restart 

Now you can view PHP errors by this way

This is an agreeable solution to this issue for me.

You can also define a specific error log file for each VirtualHost in Apache. If you have any VirtualHost defined in /etc/apache2/sites-available/ and enabled in /etc/apache2/sites-enabled (enable with sudo a2ensite [your-virtualhost-definition-file] ), you can change the error log by adding the following line inside your VirtualHost config:

ErrorLog $/[your-vhost]-error.log 

That might be useful if you have a lot of vhosts and want to split where they report the errors.

Also, you can watch your error log live by issuing the following command (adapt to your own log file if different from the default):

sudo tail -f /var/log/apache2/error.log 

This is particularly useful when doing live debugging.

Источник

Locating Apache and PHP Log Files on Ubuntu

Ubuntu 9

In this article, we will discuss how to locate Apache and PHP log files on an Ubuntu system. These log files are crucial for troubleshooting and understanding the operations of your web server.

The Apache log files on Ubuntu are typically located in the /var/log/apache2/ directory, with the primary log files being access.log and error.log . PHP log file locations can vary depending on the PHP configuration, and the location is set in the php.ini file with the error_log directive. To find the location of the php.ini file, you can use the command php —ini | grep «Loaded Configuration File» .

Understanding Apache and PHP Log Files

Apache is a popular web server that serves a significant portion of the web. PHP, on the other hand, is a widely-used scripting language that is especially suited for web development. Both these systems generate log files that provide a detailed record of events, errors, and other informational messages. The log files are essential for diagnosing problems, understanding the system’s behavior, and even for security audits. Therefore, knowing where these files are located and how to read them is a critical skill for any system administrator.

Default Locations of Apache Log Files

  • access.log : This file records all requests processed by the server. Each line in this file includes the client IP address, request date and time, HTTP method, URI, status code, and other information.
  • error.log : This file contains diagnostic information and error messages. When something goes wrong, such as a configuration error or a failed request, the server logs a descriptive message here.

You can view these files using standard text processing commands. For example, you can use the cat , less , or tail commands:

sudo tail -f /var/log/apache2/error.log

In this command, tail is used to output the last part of files. The -f option tells tail to not stop when the end of the file is reached, but rather to wait for additional data to be appended to the input.

Default Locations of PHP Log Files

The location of PHP log files can vary depending on your PHP configuration. The location is set in the php.ini file with the error_log directive.

To find out where PHP is logging errors, you can look for the error_log directive in your php.ini file. You can find the location of this file by running:

php --ini | grep "Loaded Configuration File"

This command will output the path to the loaded php.ini file. Open this file and look for the error_log directive:

grep "error_log" /etc/php/7.2/apache2/php.ini

In this command, grep is used to print lines that match a pattern. Here, it is searching for “error_log” in the php.ini file.

If the error_log directive is not set, PHP will send errors to the same place that your Apache server does.

Conclusion

Understanding and locating Apache and PHP log files is crucial for system administrators for effective troubleshooting and system management. While the default locations covered in this article will apply to most Ubuntu installations, always remember that custom configurations may alter these paths. Always refer to your specific configuration files and server documentation for the most accurate information.

You can view the Apache log files on Ubuntu by using standard text processing commands such as cat , less , or tail . For example, you can use the command sudo tail -f /var/log/apache2/error.log to view the last part of the error log file in real-time.

The default Apache log files are typically located in the /var/log/apache2/ directory on Ubuntu. The main log files you will find in this directory are access.log and error.log .

The Apache access.log file contains a record of all requests processed by the server. Each line in the file includes information such as the client IP address, request date and time, HTTP method, URI, status code, and other details.

The Apache error.log file contains diagnostic information and error messages. When something goes wrong, such as a configuration error or a failed request, the server logs a descriptive message in this file.

The location of PHP log files can vary depending on your PHP configuration. The location is set in the php.ini file using the error_log directive. You can find the path to the loaded php.ini file by running the command php —ini | grep «Loaded Configuration File» .

Источник

Where are PHP Errors Logged?

Where are PHP Errors Logged?

Where are PHP Errors Logged?

So when we encounter errors in our code, where exactly can we find them? At a high level, there are really only three places where PHP errors can be found: inline with program execution, in the system log, or in error monitoring tools like Rollbar.

Inline errors

By default, whenever an error or exception is thrown, PHP sends the error message directly to the user via STDOUT. In a command-line environment, this means that errors are rendered in the terminal. In a web environment, errors and exceptions get displayed directly in the browser.

While this behavior is useful for debugging problems in a development environment, it should be disabled in a production environment for security reasons. To do this, open up the PHP configuration file for the environment you are working in—typically found in a path that looks like /etc/php/:environment:/php.ini—and change the display_errors directive to Off.

; This directive controls whether or not and where PHP will output errors, ; notices and warnings too. Error output is very useful during development, but ; it could be very dangerous in production environments. Depending on the code ; which is triggering the error, sensitive information could potentially leak ; out of your application such as database usernames and passwords or worse. ; For production environments, we recommend logging errors rather than ; sending them to STDOUT. ; Possible Values: ; Off = Do not display any errors ; stderr = Display errors to STDERR (affects only CGI/CLI binaries!) ; On or stdout = Display errors to STDOUT ; Default Value: On ; Development Value: On ; Production Value: Off ; http://php.net/display-errors display_errors = On

Log files

While rendering errors to STDOUT is great for debugging issues in a development environment as they happen, it isn’t very useful in a production environment. This is where the error log comes into play. By default, PHP doesn’t log any errors, which means that this value must be explicitly set. To do so, open up the same PHP configuration file referenced above in your favorite editor and find the error_log directive.

; Log errors to specified file. PHP's default behavior is to leave this value ; empty. ; http://php.net/error-log ; Example: ;error_log = php_errors.log ; Log errors to syslog (Event Log on Windows). error_log = syslog

There are two possible values for error_log: a custom log file and the syslog. If the syslog is used, then all PHP errors will be sent directly to the default system log file—in Linux, this is typically /var/log/syslog. The more manageable method is to use a custom log file. By doing this, you can isolate your PHP application’s logs from the rest of the system logs, which can make debugging issues significantly easier.

Logging in Laravel

While PHP’s default system logger is useful for bespoke applications, it is important to note that many application frameworks provide their own built-in logging mechanisms. A great example of this is the Laravel framework. In Laravel, the logging method can be changed within the log option of the application configuration file—found in config/app.php.

/* |-------------------------------------------------------------------------- | Logging Configuration |-------------------------------------------------------------------------- | | Here you may configure the log settings for your application. Out of | the box, Laravel uses the Monolog PHP logging library. This gives | you a variety of powerful log handlers / formatters to utilize. | | Available Settings: "single", "daily", "syslog", "errorlog" | */ 'log' => env('APP_LOG', 'single'),

By default, Laravel maintains a single log file at storage/logs/laravel.log within the project directory, rather than the defined error_log option from the global PHP configuration.

Logging in Symfony

Because Laravel is built on top of Symfony, they share the same core logging mechanism—although the configuration differs between the two frameworks. Logging in Symfony and Laravel are both done using Monolog, a third-party PHP logging library that can be used to create and store logs in a large number of ways.

By default, Symfony logs are stored in var/log/dev.log and var/log/prod.log within the project directory, depending on the environment, but these defaults can be changed in the Monolog package configuration file found at config/packages/monolog.php.

$container->loadFromExtension('monolog', array( 'handlers' => array( 'file_log' => array( 'type' => 'stream', 'path' => '%kernel.logs_dir%/%kernel.environment%.log', 'level' => 'debug', ), 'syslog_handler' => array( 'type' => 'syslog', 'level' => 'error', ), ), ));

What do PHP logs look like?

So, what exactly do PHP logs look like? In most instances, PHP logs follow a fairly predictable format:

Dec 10 04:04:38 scotchbox apache2: PHP Parse error: syntax error, unexpected end of file in /var/www/public/index2.php on line 4

In a nutshell, the log line above can be broken up into four parts: the date, the hostname, the process, and the error message. Whenever an error is encountered or an uncaught exception is thrown, the error message is printed along with the date, hostname, and process metadata to help pinpoint what happened, where it happened, and when it happened.

A primer on log levels

It is important to note that, in PHP, there are a handful of log levels that can be squashed or raised. While these log levels are determined by PHP itself, understanding what they are and mean is a crucial step towards being able to diagnose problems as they happen.

When display_errors is set to On, it can be useful to explicitly hide and show specific log levels so you can focus on one task at a time, such as critical errors, or cleaning up warnings. This can be accomplished using the built-in error_reporting method.

// Report simple running errors error_reporting(E_ERROR | E_WARNING | E_PARSE); // Report all errors except E_NOTICE error_reporting(E_ALL & ~E_NOTICE);

This method accepts an integer value that tells PHP which errors to display, and which ones to ignore. Through the use of bitwise operators (| meaning OR, & meaning AND, and ~ meaning NOT), we can clearly and easily define which errors we want to see.

Here are a few of the most common log levels. For more information about log levels (there are quite a few of them), take a look at PHP’s official documentation.

Error Level Description
E_ERROR Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted.
E_WARNINIG Run-time warnings (non-fatal errors). Execution of the script is not halted.
E_PARSE Compile-time parse errors. Parse errors should only be generated by the parser.
E_NOTICE Run-time notices. These indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.

How to Throw Exceptions in Flutter

Источник

Читайте также:  Python no module named locale
Оцените статью