You can configure the Python agent to log output. This configuration allows the agent to track whether it is connecting to New Relic properly and if any errors occur. This information will be useful to New Relic Support if you experience problems.
debug logging can help troubleshoot your standard Python agent installation.
Some partners use different procedures:
Open your newrelic.ini, usually located within your app hierarchy.
#log_file = /tmp/newrelic-python-agent.log. Ensure you have write permissions to the log location, changing the path and file name if necessary. If there is no suitable file location, you can set
debugcan generate a lot of data very quickly. Monitor the size of your log file closely, changing
infoas you finish troubleshooting.
Save and close the file. Restart your app for the settings to take affect.
Generate a few minutes of traffic to your app.
If sending your log file to New Relic Support, attach your newrelic.ini to your support ticket, and tell Support your time zone.
When troubleshooting your New Relic Python agent, ensure it has been configured to generate debug level log files, and monitor the size of your log file closely. Using
log_level = debug generates a lot of data very quickly. After reproducing your problem, return the log file to
log_level = info.
The agent uses the Python logging module to output log messages. Output from the agent should therefore be factored into the overall logging strategy for your application.
If you are not using the logging module, the agent provides a simplified way to enable a log file for the Python agent. To use this, set the
log_level options in the agent configuration file.
log_file = /tmp/newrelic-python-agent.loglog_level = info
The path provided for
log_file should be writable to the user that your application runs as.
Apache/mod_wsgi the Apache user has restricted access to the filesystem. Create a special directory into which the log file can be placed which is writable to the Apache user. Using an absolute path, and not a relative path is recommended.
The log level used can be one of
debug. Under normal circumstances, use
info. More verbose debug options are used for
debug. Do not use these verbose debug options for an extended period of time. They can generate excessive output and the log file handler from the logging module is the standard file handler and does not perform any log file rotation.
For some hosting providers, it may not be possible to use a distinct log file for the agent. Configure the logging module to log to standard error output. This output is captured in the normal error log file for your hosting mechanism.
To do this within the agent configuration file, run:
log_file = stderrlog_level = info
stdout may also be used instead of
If you need to record and view information about all data being transmitted between the monitored process and the New Relic collector, you can enable audit logging for short periods of time. This is useful, for example, with debugging or auditing, when you need detailed information about what exactly is being transmitted.
If no logging is captured, there may be a conflict with the way in which the Python logging module is being initialized and/or configured. Problems may arise using the following functions:
Refer to the documentation of any web framework or application being used. It may provide a specialized mechanism for configuring the Python logging module. For example, when using Django, the dictionary approach to setting up the Python logging module is supported automatically, with the definitions being set in the
LOGGING attribute within the Django settings module.
For more information, see the Django logging configuration documentation.
When using the
log_file option in the agent configuration, the standard file handler from the logging module is used. This does not do any log file rotation. Log file rotation is not done automatically as we will not know what sort of rotating log file handler you may want to use, plus the standard rotating log file handlers supplied with Python are not necessarily safe for a multi process configuration. As such, it may be necessary to download and use one of the third party rotating log file handlers or use a dedicated logging system.
If your application runs in a single process you can safely use either of the
TimedRotatingFileHandler handlers supplied with the logging module. To use this only for the output from the Python agent, include at the start of your WSGI script file or module, but before the import of the
newrelic module, the following:
_LOG_FORMAT = '%(asctime)s (%(process)d/%(threadName)s)' \' %(name)s %(levelname)s - %(message)s'_logger = logging.getLogger('newrelic')_handler = logging.handlers.TimedRotatingFileHandler('agent.log', when='midnight', backupCount=7)_formatter = logging.Formatter(_LOG_FORMAT)_handler.setFormatter(_formatter)_logger.addHandler(_handler)_logger.setLevel(logging.INFO)
This code accesses the root
newrelic logger instance, attaches the rotating log file handler to it, and sets the log level for messages to be sent to this log file. We also show how to set up the log message format, but that is optional.
If the logging module as a whole is initialized, any log output will propagate up and be logged by any handler associated with the root logger, including possibly to standard error. To avoid duplicates, configure the log handler for the root logger.
The above code could also be simplified by using the
logging.fileConfig() function and a configuration file. For further details of using a configuration file see Python logging module documentation on Logging configuration.
The rotating log file handlers provided in the standard logging module are not entirely safe to use in a multi-process server configuration. Problems that can occur are intermingling of messages from multiple processes and attempts by multiple processes to perform log file rotation at the same time.
For a more robust log file rotation mechanism, use a third party log handler in conjunction with the Python logging module.
One such implementation available on PyPi is:
For a more complex solution you may also consider a logging service such as:
Also consult the list of other handlers provided as standard in the Python logging module as those for sending to a socket or posting to a HTTP URL may also be reasonable solutions in some circumstances.