Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command logging #821

Open
dilraj007 opened this issue Sep 17, 2024 · 2 comments
Open

Command logging #821

dilraj007 opened this issue Sep 17, 2024 · 2 comments

Comments

@dilraj007
Copy link

Is it possible to add config rules to add logging functionality for input/output?

Right now I'm working on Laravel project, we use a lot of tinker in production environment.
It would be great if it's possible to log all the input from our usage.

Sadlmy I can't find any tutorials/reference to do this right now.

@bobthecow
Copy link
Owner

Can you tell me a little bit more about what you're trying to accomplish?

The history command logs all your input, and you can save that to a file, or interact with the history file directly.

If you're trying to capture sessions, I'd probably use something like script :

script -c ./vendor/bin/psysh session.log
# do some stuff, then exit
cat session.log

@dilraj007
Copy link
Author

dilraj007 commented Sep 23, 2024

I want to log every command input in Tinker shell.

Real time logging, directly into laravel.log.

Actually, I've managed to do it by creating custom function like this.

<?php

namespace App\Console\Commands;

use Laravel\Tinker\Console\TinkerCommand;
use App\Console\LoggingShell;
use Psy\Configuration;
use Laravel\Tinker\ClassAliasAutoloader;
use Illuminate\Support\Env;

class LoggingTinkerCommand extends TinkerCommand
{
    public function handle()
    {
        $this->getApplication()->setCatchExceptions(false);

        $config = Configuration::fromInput($this->input);
        $config->setUpdateCheck(\Psy\VersionUpdater\Checker::NEVER);

        $config->getPresenter()->addCasters(
            $this->getCasters()
        );

        if ($this->option('execute')) {
            $config->setRawOutput(true);
        }

        $shell = new LoggingShell($config);
        $shell->addCommands($this->getCommands());
        $shell->setIncludes($this->argument('include'));

        $path = Env::get('COMPOSER_VENDOR_DIR', $this->getLaravel()->basePath().DIRECTORY_SEPARATOR.'vendor');

        $path .= '/composer/autoload_classmap.php';

        $config = $this->getLaravel()->make('config');

        $loader = ClassAliasAutoloader::register(
            $shell, $path, $config->get('tinker.alias', []), $config->get('tinker.dont_alias', [])
        );

        if ($code = $this->option('execute')) {
            try {
                $shell->setOutput($this->output);
                $shell->execute($code);
            } finally {
                $loader->unregister();
            }

            return 0;
        }

        try {
            return $shell->run();
        } finally {
            $loader->unregister();
        }
    }
}
<?php

namespace App\Console;

use Psy\Shell;
use Illuminate\Support\Facades\Log;

class LoggingShell extends Shell
{
    protected function readline(bool $interactive = true)
    {
        $line = parent::readline($interactive);
        if ($line !== false) {
            Log::info('Tinker cmd: ' . $line);
        }
        return $line;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants