-
Notifications
You must be signed in to change notification settings - Fork 7
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
adding logging to io.genomio.fasta.process #206
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
|
@@ -16,3 +16,4 @@ | |
|
||
from .archive_utils import * | ||
from .json_utils import * | ||
from .logging import * |
This file contains 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,79 @@ | ||
# See the NOTICE file distributed with this work for additional information | ||
# regarding copyright ownership. | ||
# | ||
# 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. | ||
"""Utils to deal with logging.""" | ||
|
||
__all__ = ["setup_logging"] | ||
|
||
import argparse | ||
import logging | ||
from typing import Optional | ||
|
||
|
||
# default logging formats | ||
LOGGING_FORMAT = "%(asctime)s\t%(levelname)s\t%(message)s" | ||
DATE_FORMAT = r"%Y-%m-%d_%H:%M:%S" | ||
|
||
# helper functions | ||
def _prepare_console_handler(*, debug: bool = False, verbose: bool = False) -> logging.StreamHandler: | ||
"""setup console handler with different logging levels""" | ||
console_h = logging.StreamHandler() | ||
# set default logging level | ||
if debug: | ||
console_h.setLevel(logging.DEBUG) | ||
elif verbose: | ||
console_h.setLevel(logging.INFO) | ||
return console_h | ||
|
||
|
||
def _prepare_file_handler(filename: Optional[str] = None, *, debug: bool = False) -> Optional[logging.FileHandler]: | ||
"""setup file handler with default loggin.INFO level""" | ||
"""retuns None if no file name defined""" | ||
if filename is None: | ||
return None | ||
|
||
file_h = logging.FileHandler(filename) | ||
|
||
file_h.setLevel(logging.INFO) | ||
if debug: | ||
file_h.setLevel(logging.DEBUG) | ||
|
||
return file_h | ||
|
||
|
||
def setup_logging( | ||
args: argparse.Namespace, | ||
*, # no positional arguments allowed after this | ||
name: Optional[str] = None, | ||
) -> logging.Logger: | ||
"""Setup logging infrustucture.""" | ||
"""args: argparse.Namespace -- args with "debug", "verbose" and "logfile" options.""" | ||
# get logger with a specified name (or the default one) | ||
logger = logging.getLogger(name) | ||
|
||
# set up shared formatter | ||
formatter = logging.Formatter(fmt = LOGGING_FORMAT, datefmt = DATE_FORMAT) | ||
|
||
# adding console handler | ||
console_h = _prepare_console_handler(debug = args.debug, verbose = args.verbose) | ||
console_h.setFormatter(formatter) | ||
logger.addHandler(console_h) | ||
|
||
# adding logging to file | ||
file_h = _prepare_file_handler(args.logfile) | ||
if file_h: | ||
file_h.setFormatter(formatter) | ||
logger.addHandler(file_h) | ||
|
||
return logger |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or we can just replace
logger
withlogging
and do something likeOr make a more cruel thing.
Just treat
import logging
as definition of a global var. So justThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was not happy with the
logger
approach either, so that is why I have opted for accessing thelogging.root
instead in my PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the reassigning of
logging
way,because in this case you're allowing the module to be used/imported from the outside without calling main function and setup we have in it.
So if you use main -- you define your own logger (however prettified) and assign it to global
logging
(replacing it, basically).But if you import this module from outside, you preserve logging default settings (perhaps, already modified by the calling/upstream code).
In this case this module itself doesn't import other models with logging.
I think, it's worth to update the root logger to propagate changes downstream, instead as in your PR !207.