Get remote laravel log file

By now you are deploying your web applications via some sort of magic like Codeship and have probably decided to turn FTP off on your servers. Either that or you are using Forge and the only way you can get at your production server is via SSH.

If you want to run migrations on your production server then you can SSH in or maybe have an Envoy command to do this. However, what happens if you need to review your production log files from your Laravel app but you cannot just FTP in and download them.

At first I thought about doing it via Envoy but I could not get my head around how to use the ‘scp’ command. I ended up setting an alias to do it from my local machine using

alias get-amazing-com-log=”scp username@server.com:/home/web/amazing.com/app/storage/logs/laravel.log ~/Projects/amazing_com/app/storage/logs/”

Then I thought, how about a quick Laravel Command to do something like

php artisan logs:get

To do this we are going to make use of the SSH Class in Laravel, specifically SFTP Downloads. For us to be able to use this class we need to set some remote configurations.

To get started we need to go to ‘/app/config/remote.php’ and add the settings for your remote server, in this example we will set them for our production server.

<?php
// app/config/remote/php
'connections' => array(
    'production' => array(
        'host' => $_ENV['SSH_HOST'],
        'username' => $_ENV['SSH_USERNAME'],
        'password' => $_ENV['SSH_PASSWORD'],
        'key' => '',
        'keyphrase' => '',
        'root' => '/home/web/amazing.com',
    ),
),

You will notice that the only thing we actually set in this file is the ‘root’ as everything else is extremely sensitive so we are going to set those configuration items using “dot” files.

<? // .env.local.php return array( 'SSH_HOST' => 'server.com',
'SSH_USERNAME' => 'deployment',
'SSH_PASSWORD' => '53curep455w0rd',
);

Once we have the configuration setup then we are ready to setup our new Artisan Command.

<?php
// app/commands/GetLog.php

use IlluminateConsoleCommand;
use SymfonyComponentConsoleInputInputArgument;

class GetLog extends Command
{

    protected $name = 'logs:get';
    protected $description = 'Connect via SSH to the given remote environment and download log file.';

    public function __construct()
    {
        parent::__construct();
    }

    public function fire()
    {

        $this->info('Stand by...');

        try {

            $env = $this->argument('env');
            $app_log_path = '/app/storage/logs/laravel.log';
            $remote_log_path = Config::get('remote.connections.' . $env . '.root') . $app_log_path;
            $local_log_path = dirname(__FILE__) . '/../storage/logs/' . $env . '.log';

            SSH::into($env)->get($app_log_path, $local_log_path);

        } catch (InvalidArgumentException $e) {

            $this->error('Check you have settings for app/config/remote.php connections.' . $env);
            exit;

        }

        $this->info('Environment : ' . $env);
        $this->info('Connected to ' . $_ENV['SSH_HOST']);
        $this->info('Getting file from : ' . $remote_log_path);
        $this->info('File downloaded to : ' . $local_log_path);

    }

    protected function getArguments()
    {
        return array(
            array(
                'env',
                InputArgument::OPTIONAL,
                'Environment to get the logs from set in app/config/remote.',
                'production'
            ),
        );
    }
}

And remember to register this command so you have access to it when running Artisan on the CLI.

<?php

// app/start/artisan.php

Artisan::add(new GetLog);

And there you have it. Now you can run a simple Artisan command from your local machine or from within your Homestead VM and it will connect to production, pull down the log file and store it in ‘app/storage.logs/production.log’.

You’re welcome.

7 thoughts on “Get remote laravel log file

    • Yer, I tail the log file when I need to watch something. This method does just SSH into the machine but it’s wrapping it in a helpful artisan command to save time when you need to review an entire log file. Thanks for reading, and the comment.

Leave a Reply

Your email address will not be published. Required fields are marked *