-
Notifications
You must be signed in to change notification settings - Fork 154
Add Other Logging Implementations #858
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
Merged
christophebedard
merged 6 commits into
ros2:rolling
from
InvincibleRMC:add-generic-log-implementation
Apr 28, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d520596
Add other logging implementations
InvincibleRMC b28e2de
Merge branch 'ros2:rolling' into add-generic-log-implementation
InvincibleRMC cbb54ce
Address feedback
InvincibleRMC 2e73659
Update launch/launch/actions/log_info.py
InvincibleRMC b5cad75
Use pytest.raises
InvincibleRMC eadc495
Fix level_error test
InvincibleRMC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# Copyright 2025 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Module for the Log action.""" | ||
|
||
import logging | ||
from typing import List | ||
import warnings | ||
|
||
import launch.logging | ||
|
||
from ..action import Action | ||
from ..frontend import Entity | ||
from ..frontend import expose_action | ||
from ..frontend import Parser # noqa: F401 | ||
from ..launch_context import LaunchContext | ||
from ..some_substitutions_type import SomeSubstitutionsType | ||
from ..substitution import Substitution | ||
from ..utilities import normalize_to_list_of_substitutions | ||
|
||
|
||
@expose_action('log') | ||
class Log(Action): | ||
"""Action that logs a message when executed.""" | ||
|
||
def __init__(self, *, msg: SomeSubstitutionsType, | ||
level: SomeSubstitutionsType, **kwargs): | ||
"""Create a Log action.""" | ||
super().__init__(**kwargs) | ||
|
||
self.__msg = normalize_to_list_of_substitutions(msg) | ||
self.__level = normalize_to_list_of_substitutions(level) | ||
self.__logger = launch.logging.get_logger('launch.user') | ||
|
||
@classmethod | ||
def parse( | ||
cls, | ||
entity: Entity, | ||
parser: 'Parser' | ||
): | ||
"""Parse `log` tag.""" | ||
_, kwargs = super().parse(entity, parser) | ||
kwargs['msg'] = parser.parse_substitution(entity.get_attr('message')) | ||
|
||
# Check if still using old log action | ||
level = entity.get_attr('level', optional=True) | ||
# TODO: Remove optional level for Release after L-turtle release | ||
if level is None: | ||
warnings.warn( | ||
'The action log now expects a log level.' | ||
' Either provide one or switch to using the log_info action', | ||
stacklevel=2) | ||
level = 'INFO' | ||
|
||
kwargs['level'] = parser.parse_substitution(level) | ||
return cls, kwargs | ||
|
||
@property | ||
def msg(self) -> List[Substitution]: | ||
"""Getter for self.__msg.""" | ||
return self.__msg | ||
|
||
@property | ||
def level(self) -> List[Substitution]: | ||
"""Getter for self.__level.""" | ||
return self.__level | ||
|
||
def execute(self, context: LaunchContext) -> None: | ||
"""Execute the action.""" | ||
level_sub = ''.join([context.perform_substitution(sub) | ||
for sub in self.level]).upper() | ||
|
||
level_map = logging.getLevelNamesMapping() | ||
if level_sub not in level_map: | ||
raise KeyError(f"Invalid log level '{level_sub}', expected: {level_map.keys()}") | ||
|
||
level_int = level_map[level_sub] | ||
|
||
self.__logger.log(level_int, | ||
''.join([context.perform_substitution(sub) for sub in self.msg]) | ||
) | ||
return None | ||
|
||
|
||
@expose_action('log_info') | ||
class LogInfo(Log): | ||
"""Action that logs a message with level INFO when executed.""" | ||
|
||
def __init__(self, *, msg: SomeSubstitutionsType, **kwargs): | ||
"""Create a LogInfo action.""" | ||
kwargs.pop('level', None) | ||
super().__init__(msg=msg, level='INFO', **kwargs) | ||
|
||
|
||
@expose_action('log_warning') | ||
class LogWarning(Log): | ||
"""Action that logs a message with level WARNING when executed.""" | ||
|
||
def __init__(self, *, msg: SomeSubstitutionsType, **kwargs): | ||
"""Create a LogWarning action.""" | ||
kwargs.pop('level', None) | ||
super().__init__(msg=msg, level='WARNING', **kwargs) | ||
|
||
|
||
@expose_action('log_debug') | ||
class LogDebug(Log): | ||
"""Action that logs a message with level DEBUG when executed.""" | ||
|
||
def __init__(self, *, msg: SomeSubstitutionsType, **kwargs): | ||
"""Create a LogDebug action.""" | ||
kwargs.pop('level', None) | ||
super().__init__(msg=msg, level='DEBUG', **kwargs) | ||
|
||
|
||
@expose_action('log_error') | ||
class LogError(Log): | ||
"""Action that logs a message with level ERROR when executed.""" | ||
|
||
def __init__(self, *, msg: SomeSubstitutionsType, **kwargs): | ||
"""Create a LogError action.""" | ||
kwargs.pop('level', None) | ||
super().__init__(msg=msg, level='ERROR', **kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Copyright 2020 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Tests for the Log action classes.""" | ||
|
||
from launch import LaunchContext | ||
from launch.actions import Log | ||
from launch.actions import LogDebug | ||
from launch.actions import LogError | ||
from launch.actions import LogInfo | ||
from launch.actions import LogWarning | ||
from launch.utilities import perform_substitutions | ||
|
||
import pytest | ||
|
||
|
||
def test_log_constructors(): | ||
"""Test the constructors for Log classes.""" | ||
Log(msg='', level='INFO') | ||
Log(msg='', level='DEBUG') | ||
Log(msg='foo', level='WARNING') | ||
Log(msg=['foo', 'bar', 'baz'], level='ERROR') | ||
|
||
LogDebug(msg='') | ||
LogDebug(msg='foo') | ||
LogDebug(msg=['foo', 'bar', 'baz']) | ||
|
||
LogError(msg='') | ||
LogError(msg='foo') | ||
LogError(msg=['foo', 'bar', 'baz']) | ||
|
||
LogInfo(msg='') | ||
LogInfo(msg='foo') | ||
LogInfo(msg=['foo', 'bar', 'baz']) | ||
|
||
LogWarning(msg='') | ||
LogWarning(msg='foo') | ||
LogWarning(msg=['foo', 'bar', 'baz']) | ||
|
||
|
||
def test_log_methods(): | ||
"""Test the methods of the LogInfo class.""" | ||
launch_context = LaunchContext() | ||
|
||
log = Log(msg='', level='INFO') | ||
assert perform_substitutions(launch_context, log.msg) == '' | ||
|
||
log = Log(msg='foo', level='INFO') | ||
assert perform_substitutions(launch_context, log.msg) == 'foo' | ||
|
||
log = Log(msg=['foo', 'bar', 'baz'], level='INFO') | ||
assert perform_substitutions(launch_context, log.msg) == 'foobarbaz' | ||
|
||
log = Log(msg=['foo', 'bar', 'baz'], level=['I', 'N', 'F', 'O']) | ||
assert perform_substitutions(launch_context, log.level) == 'INFO' | ||
|
||
log = LogDebug(msg='') | ||
assert perform_substitutions(launch_context, log.level) == 'DEBUG' | ||
|
||
log = LogError(msg='') | ||
assert perform_substitutions(launch_context, log.level) == 'ERROR' | ||
|
||
log = LogInfo(msg='') | ||
assert perform_substitutions(launch_context, log.level) == 'INFO' | ||
|
||
log = LogWarning(msg='') | ||
assert perform_substitutions(launch_context, log.level) == 'WARNING' | ||
|
||
|
||
def test_log_execute(): | ||
"""Test the execute (or visit) of the LogInfo class.""" | ||
log = Log(msg='foo', level='ERROR') | ||
launch_context = LaunchContext() | ||
assert log.visit(launch_context) is None | ||
|
||
|
||
def test_log_level_error(): | ||
"""Checks for error message to be raised given invalid level.""" | ||
launch_context = LaunchContext() | ||
with pytest.raises(KeyError, match=r'Invalid log level*'): | ||
Log(msg='foo', level='foo').execute(launch_context) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.