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

[5.2] Use an injected logger in the Task driver #36440

Draft
wants to merge 7 commits into
base: 5.2-dev
Choose a base branch
from

Conversation

ditsuke
Copy link
Contributor

@ditsuke ditsuke commented Dec 27, 2021

Summary of Changes

A logger is injected into Task at creation, and into routines through ExecutedTaskEvent at runtime.

Testing Instructions

Tasks should be logged as expected to the scheduler log file "administrator/logs/joomla_scheduler.php".
Logs for individual tasks, if enabled, should be saved to the file as specified in the configuration.

Actual result BEFORE applying this Pull Request

No DI, ugly setup code in TaskPluginTrait etc.

Expected result AFTER applying this Pull Request

DI, less static calls (still depends on the global Log instance to setup sub-loggers! :/), more succinct.

Documentation Changes Required

-

- Modifies the Task constructor to accept a LoggerInterface instance.
- Updates Task creation in Scheduler::getTask().
- ExecuteTaskEvent carries the task logger as a callable arg.
- TaskPluginTrait::startRoutine() sets up callable.
- TaskPluginTrait::logTask() uses injected logger.
Logger setup is done in the Scheduler and Task classes.
@@ -255,7 +260,7 @@ public function getTask(array $options = []): ?Task
return null;
}

return new Task($task);
return new Task($task, Factory::getContainer()->get(LoggerInterface::class));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if you can get here a logger which is injected all the way down from the component class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not able to figure how to do this, since our instantiation chain starts from a plugin. Any leads? 😕

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you boot the scheduler component in the plugin?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, its only done in the plugin's injectLazyJS() method. The component is otherwise booted within the Scheduler::getTask() method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be injecting the model/component instance?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes there you can start with it. In the provider.php file you can inject the logger into the component and then pass it into the scheduler.

'text_file' => 'joomla_scheduler.php'
];
/** ! No non-static methods to add a sub-logger to {@see DelegatingPsrLogger} (Task::logger) */
Log::addLogger($loggerOptions, Log::ALL, $task->logCategory);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better inject a pre-configured logger using Scheduler's constructor. Within the component, you shouild just send messages to the logger and not change its configuration. The setup is up to the front controller, not the component itself.

For example, if one wants to set up an automated evaluation of the log, it is necessary to be able to freely determine the log format and the storage of the log data. Your approach, however, prescribes these properties so that the dependency injection is practically worthless.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the merit in decoupling the configuration! It's totally doable for everything but task-specific logging since the relevant information is only exposed once we get the task record from the database. What would be the best way to do that setup?

https://github.com/ditsuke/joomla-cms/blob/scheduler-logging-di/administrator/components/com_scheduler/src/Task/Task.php#L151-L160

This comment was marked as duplicate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can, but it needs some additional configuration specific to the task (logging to the task-specific log file).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a misconception. If, how and where things are logged is totally up to the system admin. If you want (feel the need) to provide a default logging procedure, you can do so by providing a bespoke logger that is instantiated as a default, if no logger is provided be the environment.

$app = $this->app ?? Factory::getApplication();
$app->getLanguage()->load('com_scheduler', JPATH_ADMINISTRATOR);
$langLoaded = true;
throw new \LogicException("Logger has not been setup!");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging should always be optional. Issue a notice or warning, if necassary, but don't stop execution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Didn't think of that

'text_file' => 'joomla_scheduler.php'
];
/** ! No non-static methods to add a sub-logger to {@see DelegatingPsrLogger} (Task::logger) */
Log::addLogger($loggerOptions, Log::ALL, $task->logCategory);

This comment was marked as duplicate.

@ditsuke ditsuke marked this pull request as draft January 1, 2022 06:38
@HLeithner HLeithner changed the base branch from 4.1-dev to 4.2-dev June 27, 2022 13:06
@HLeithner
Copy link
Member

This pull request has automatically rebased to 4.2-dev.

@laoneo
Copy link
Member

laoneo commented Oct 21, 2022

@ditsuke can you implement the the suggestions and fix the conflicts?

@Hackwar Hackwar added the Feature label Apr 6, 2023
@HLeithner HLeithner changed the base branch from 4.2-dev to 5.0-dev May 2, 2023 16:42
@HLeithner
Copy link
Member

This pull request has been automatically rebased to 5.0-dev.

@HLeithner HLeithner changed the base branch from 5.0-dev to 5.1-dev September 30, 2023 22:52
@HLeithner
Copy link
Member

This pull request has been automatically rebased to 5.1-dev.

@HLeithner HLeithner changed the base branch from 5.1-dev to 5.2-dev April 24, 2024 09:09
@HLeithner
Copy link
Member

This pull request has been automatically rebased to 5.2-dev.

@HLeithner HLeithner changed the title [4.1] Use an injected logger in the Task driver [5.2] Use an injected logger in the Task driver Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants