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

Logger and persistence problem #42

Closed
BasvanH opened this issue Nov 5, 2019 · 4 comments
Closed

Logger and persistence problem #42

BasvanH opened this issue Nov 5, 2019 · 4 comments

Comments

@BasvanH
Copy link
Contributor

BasvanH commented Nov 5, 2019

I have extended the Logger of Rubix to Laravel Log facade. This works very well except when you start to use persistence.

$estimator->setLogger(new class extends Logger {
  public function log($level, $message, array $context = array())
   {
     Log::$level($message, $context);
   }
});

When I try to save the estimator, I get this error:
Serialization of 'class@anonymous' is not allowed
vendor/rubix/ml/src/Persisters/Serializers/Native.php:26

Can't seem to figure out why this is happening. Logger should not be near the persistence I think.

@andrewdalpino
Copy link
Member

andrewdalpino commented Nov 5, 2019

Hi @BasvanH

The logger is persisted along with the model data - this is done so that it continues logging after being reconstituted from storage

Unfortunately, however, there is a language-wide restriction that anonymous classes (as well as anonymous functions) cannot be deserialized (see this page). If you think about it, there is no way for the program to know what to do with the instance data if it cannot attach it to a class definition.

If you want to be able to persist the logger along with the rest of the model data, then just turn it into a regular class and it should work fine.

Since we use the PSR-3 logging standard - any compatible logger should work including Monolog and perhaps even the Laravel logger (not tested).

I think it would be a good idea to add a note about this to the docs here.

@andrewdalpino
Copy link
Member

andrewdalpino commented Nov 5, 2019

Added note to the docs about this limitation 0ca542d

Thank you for bringing this to my attention

@BasvanH
Copy link
Contributor Author

BasvanH commented Nov 11, 2019

Alright, always nice to learn new things :-)

I've created my own class which forwards the logs to Laravel's Log facade. And this works perfectly.

use Rubix\ML\Other\Loggers\Logger;
use Illuminate\Support\Facades\Log;

/**
 * Forward logs from Rubix to Monolog
 */
class SimpleLogger extends Logger {
    public function log($level, $message, array $context = array())
    {
        Log::$level($message, $context);
    }
}

And set the estimator:

$estimator->setLogger(new SimpleLogger);

@BasvanH BasvanH closed this as completed Nov 11, 2019
@andrewdalpino
Copy link
Member

andrewdalpino commented Nov 11, 2019

@BasvanH You can also use Monolog directly with a Verbose estimator if you want to

Also, any comment on this enhancement for time-series support would be great #49

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