How to Access the Plex Server Logs in Real-Time

When troubleshooting issues with Plex you will probably need to access the Plex log files located in the data directory. This requires access to the server, which you may not have access to at the time of the issue.

You can also pull up the console log in the Plex settings, which is a more convenient solution.

This does have the added burden of accessing Plex via a Web browser and navigating through the Plex settings to view the console logs.

On mobile, the Plex server isn't as friendly as a desktop, so navigating and reading the console logs isn't ideal.

Another, more viable option on mobile is to install Plex Dash (App Store, Google Play) on your mobile device and view the console logs from that app. Since Plex Dash is a mobile app, the logs can easily be viewed from a mobile device.

The last option is to build something yourself. Plex provides a notifications API endpoint for viewing the Plex logs in real-time.

This option provides you with the most flexibility as you can do what you would like with the log information from Plex.

In this post, I will show you a simple Python script that you can use to establish a Websocket connection to your Plex server and display, in real-time, the Plex logs in a console window.

How to Access the Plex Server Logs in Real-Time

Displaying the Plex logs in real-time

The Python script to display the Plex logs in real-time involves making a call using a single Plex API command.

gd

Unlike most Plex API endpoints, the one to access the logs in real-time establishes a Websocket connection to the Plex server and not a standard HTTP or HTTPS connection.

The API endpoint is the Listen for Notifications endpoint.

Once the call to the endpoint is made, a Websocket connection will be established and Plex will send any logging to the client.

The script to display Plex logs in real-time is shown below.

import asyncio
import websockets

# Get the Plex URL and token from the environment variables
plex_url = os.environ.get('PLEX_URL')
plex_token = os.environ.get('PLEX_TOKEN')

async def stream_request():
    uri =f"wss://{plex_url}/:/websockets/notifications?filters=log&{plex_token}"
    try:
        # Connect to the WebSocket server
        async with websockets.connect(uri) as websocket:

            # Start listening to incoming messages (streaming)
            while True:
                response = await websocket.recv()  # Wait for the next message from the server
                print(f"Received: {response}")                

    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    # Run the WebSocket streaming
    asyncio.get_event_loop().run_until_complete(stream_request())

How to use the script

To use the script, you will need to do the following:

  1. Install Python.
  2. After Python is installed, run the following pip command to install the dependencies:
    pip install websockets
  3. Copy the above script and save it as a Python file, for example, console_log.py
  4. Create an environment variable called PLEX_URL and set it to the URL of your Plex server. For example: http://localhost:32400.
  5. Create an environment variable called PLEX_TOKEN and set it to your Plex token.

What does the script do?

The above script will establish a Websocket connection to your Plex server and will display the Plex log information until the script is stopped.

It does this using the following steps:

  1. It establishes an asynchronous event loop and calls the stream_request method.
  2. The stream_request method will establish the asynchronous Websocket connection to your Plex server.
  3. While the connection is open, it will wait for a log message, and once one is received, the script will display the log message in the terminal window.

The above script is basic in that it will just display the log message to the console window.

But you can enhance the script in any way you wish.

You may want to also log the messages to a database, or send the information to a syslog server. You can store them in a CSV file and open it in Excel to filter and sort the messages.

You can even create a Web app that displays the logging on a Web page. If you have a dashboard, you can output it there.

The point is the script can allow you to manage the logging from Plex in any manner you wish.

Photo of Paul Salmon
Started managing a Plex server in November 2014 and has been sharing his experience and what he has learned on Plexopedia. He is exploring and documenting the Plex API to help automate tasks for Plex to reduce the management effort of his server.
Subscribe
Display