Level up with logs!
When doing custom instrumentation in the way outlined in our last post, for backend code without a web frontend, New Relic logs in context will not automatically be configured by an agent. Instead, you must manually configure logs by first enabling log decoration and forwarding in the newrelic.ini file.
# This setting enables log decoration, the forwarding of log events,
# and the collection of logging metrics if these sub-feature
# configurations are also enabled. If this setting is false, no
# logging instrumentation features are enabled. This can also be
# set using the NEW_RELIC_APPLICATION_LOGGING_ENABLED environment
# variable.
application_logging.enabled = true
# If true, the agent captures log records emitted by your application
# and forwards them to New Relic. `application_logging.enabled` must
# also be true for this setting to take effect. You can also set
# this using the NEW_RELIC_APPLICATION_LOGGING_FORWARDING_ENABLED
# environment variable.
application_logging.forwarding.enabled = true
Next, you will need to manually decorate your code with the desired logging. Here’s some sample code:
For the Data Driven Dave example, a more meaningful log message might be to log a message at the start of a new game.
You could also incorporate variables into your log messages. For example, you could log a message at the end of the game with the player’s final score.
To learn more about python logging and to see more examples, check out Logging in Python.
Once you’ve decorated your code and enabled log forwarding in your newrelic.ini file, you will start to see log data showing up in the New Relic logs screen when you play the game.
Not only will you see your logs in the main log management screen along with all your other log data, you’ll see Data Driven Dave specific logs embedded within the APM UI.
Take note that the attributes you see in the log details here — entity.guid, entity.name, hostname, etc. — are derived from metadata that the Python agent API is automatically adding to your log data.
If you are a nonprofit organization, looking to level up your Observability tools, sign up for a free account here:
https://newrelic.com/social-impact/signup
New Relic offers free tools and full platform features along with 1TB of free monthly data injest and 5 user accounts to all qualifying global nonprofit customers! Just email us at hgruber(at)newrelic(dot)com to find out more!