Multiple hosts on Log Viewer

Multiple hosts on Log Viewer

This feature is available since Log Viewer v2.2. This feature also requires guzzlehttp/guzzle version 7.2 or higher installed (usually comes with Laravel by default).

If you have Log Viewer installed on multiple hosts (such as testing, staging and production servers, or different apps), you can use a single Log Viewer instance to view Laravel logs from other servers/apps.

When navigating the logs for a selected host, the requests are then proxied to that host, where the host's Log Viewer instance processes the request like it would normally and returns the result back to your current Log Viewer instance.

Setting up hosts

There are two ways to set up the hosts - via config/log-viewer.php configuration file, or setting a custom host resolver. Let's explore both options below.

Using configuration file

Before you're able to configure this, make sure to first publish the configuration file.

By default, a local host is included already:

    'hosts' => [        'local' => [            'name' => 'Local',            'host' => null,        ],        // add more hosts here    ],

Here, local is the identifier of the host (which will appear in the URL when browsing logs for that host), Local is the displayed in the UI, and host is the URL to the remote Log Viewer instance. For local instances, the host must be null.

Now, let's add another host as an example, like your production server:

    'hosts' => [        'local' => [            'name' => 'Local',            'host' => null,        ],        'production' => [            'name' => 'Production',            'host' => '',        ],    ],    

Here the host points to the remote Log Viewer instance (like you would enter it in the browser).

Authenticating hosts

A lot of times you will have your Log Viewer instances protected and will need to authenticate somehow. For this reason, there's a couple of configuration options available for the hosts.

Keep in mind that the scheme (http, https, etc) of the host attribute must be correct so there's no redirects. Otherwise, any authentication configured may get lost during redirect.

Bearer Token auth

You can add a Bearer token authentication like so:

    'hosts' => [        //        'production' => [            'name' => 'Production',            'host' => '',+           'auth' => [+               'token' => env('LOG_VIEWER_PRODUCTION_TOKEN'),+           ],        ],    ],    

Do not commit your actual tokens/keys into git. Use Laravel Environment Variables to set up tokens safely.

HTTP Basic auth

You can also use HTTP Basic authentication like so:

    'hosts' => [        //        'production' => [            'name' => 'Production',            'host' => '',+           'auth' => [+               'username' => env('LOG_VIEWER_PRODUCTION_USERNAME'),+               'password' => env('LOG_VIEWER_PRODUCTION_PASSWORD'),+           ],        ],    ],    

Additional headers

If you need to pass additional headers to the host, either for authentication or other purposes, you can do so with the headers property:

    'hosts' => [        //        'production' => [            'name' => 'Production',            'host' => '',+           'headers' => [+               'X-Foo' => 'Bar,+               'Authorization' => 'key',+           ],        ],    ],    

Using a custom resolver

If you have a more complex process of figuring out how the hosts should be configured, you can pass a callback to the LogViewer::resolveHostsUsing($callback) method inside your AppServiceProvider class:

use Opcodes\LogViewer\Facades\LogViewer;use Opcodes\LogViewer\HostCollection;/** * Bootstrap any application services. * * @return void */public function boot(){    LogViewer::resolveHostsUsing(function (HostCollection $hosts) {        // The $hosts argument provides the hosts resolved from the        // config/log-viewer.php, but you don't need to use that,        // or even return a HostCollection        return [            'local' => [                'name' => 'Local logs',            ],            'staging' => [                'name' => 'Staging server',                'host' => '',            ],        ];    });}

Using this resolver callback you can use whatever source you need to fetch the host configurations - database, cache, file, etc.

You can also just make modifications to the existing HostCollection:

use Opcodes\LogViewer\Facades\LogViewer;use Opcodes\LogViewer\HostCollection;use Opcodes\LogViewer\Host;/** * Bootstrap any application services. * * @return void */public function boot(){    LogViewer::resolveHostsUsing(function (HostCollection $hosts) {        return $hosts->map(function (Host $host) {            $host->headers['X-Foo'] = 'Bar';            return $host;        });    });}


If you have any questions, feedback, or need any help setting up Log Viewer within your project, feel free to reach out via GitHub Discussions or GitHub Issues .