Jump to content

Basic Emby Server Log Follower (Linux)


sydlexius

Recommended Posts

sydlexius

All,

I was looking for a slightly-better-than tail -f approach for the Emby log files.  I'm no expert with AWK, but played around with ChatGPT to put something basic together.  It highlights entries such as datetime, log level, message source, and message using different colors.  For the log level, there are unique colors for INFO, WARNING, DEBUG, and ERROR.  In the message, it attempts to highlight the "Time:" value, assigning colors for execution time (green is 0-1000ms, yellow for 1000-10000ms, and red for anything slower).  I realize these are not only subjective, but context-sensitive. For now, I'm too lazy/unskilled to contextualize this.  If you improve the code, please re-share it.  If requested, I'll setup a repo on GitHub.

#!/bin/bash

# Check if a log file was provided as an argument
if [ -z "$1" ]; then
    echo "Usage: logfollow <logfile>"
    exit 1
fi

# Store the log file name in a variable
LOGFILE="$1"

# Start tailing the log file and process each line using awk
tail -f "${LOGFILE}" | awk -F ':' '
BEGIN {
    # Define color codes for terminal output
    grey = "\033[0;37m";
    white = "\033[1;37m";
    green = "\033[1;32m";
    yellow = "\033[1;33m";
    red = "\033[1;31m";
    blue = "\033[1;34m";
    reset = "\033[0m";
    debug_color = "\033[1;90m";
}
{
    # Reconstruct the input line with a colon as the field separator
    original_line = $0;
    for (i = 2; i <= NF; i++) {
        original_line = original_line ":" $i;
    }

    # Split the input line into fields
    timestamp = $1;
    loglevel = $2;
    source = $3;
    message = substr(original_line, length($1 $2 $3) + 4);

    # Assign log level color based on severity
    if (loglevel == "Info") {
        loglevel_color = green;
    } else if (loglevel == "Warning") {
        loglevel_color = yellow;
    } else if (loglevel == "Error") {
        loglevel_color = red;
    } else if (loglevel == "Debug") {
        loglevel_color = debug_color;
    } else {
        loglevel_color = grey;
    }

    # Find and colorize "Time:" and its value based on the range
    time_pattern = "Time: [0-9]+ms.";
    if (match(message, time_pattern)) {
        time_string = substr(message, RSTART, RLENGTH);  # Extract the matched time string
        split(time_string, time_parts, " ");             # Split the time string into parts
        time_value = time_parts[2];                      # Get the time value in milliseconds
        gsub("ms.", "", time_value);                     # Remove "ms." from the time value
        time_value += 0;                                 # Convert the time value to a number

        # Assign a color based on the time value range
        if (time_value >= 0 && time_value <= 1000) {
            time_color = green;
        } else if (time_value > 1000 && time_value <= 10000) {
            time_color = yellow;
        } else {
            time_color = red;
        }

        # Build the colored time string
        colored_time = sprintf("%s%s%s %s%s%s", white, "Time:", reset, time_color, time_value "ms.", reset);
        sub(time_pattern, colored_time, message);       # Replace the original time string with the colored one
    }

    # Colorize the output and print the formatted line
    printf "%s%s%s %s%s%s %s%s%s %s%s%s\n",
        grey, timestamp, reset,
        loglevel_color, loglevel, reset,
        blue, source, reset,
        grey, message, reset
}'

Finally, in my case I created an alias to the logfollow.sh file.  This isn't required, but helps make the "Usage" warning make more sense.

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...