-
Notifications
You must be signed in to change notification settings - Fork 97
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
added log config and replaced print with log #235
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Please accept Google's CLA in order to proceed. |
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.
PTAL at my comments.
src/gcp_scanner/scanner.py
Outdated
@@ -166,7 +169,7 @@ def crawl_loop(initial_sa_tuples: List[Tuple[str, Credentials, List[str]]], | |||
|
|||
project_id = project['projectId'] | |||
project_number = project['projectNumber'] | |||
print(f'Inspecting project {project_id}') | |||
logging.info(f'Inspecting project {project_id}') |
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.
We actually want this to be printed in console so user understand what's going on.
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 think using logging for everything makes sense here and have the default log level be info. The output is metadata about the process, not the result of the process itself.
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 think using logging for everything makes sense here and have the default log level be info. The output is metadata about the process, not the result of the process itself.
Will there always be a logging file as part of the arguments? coz I was thinking if that file name is optional and not mandatory we can separate the logging as a filehandle for file logging and console handler for console printing
something like this
import os
import logging
def main():
log_level = getattr(logging, 'INFO', None)
log_format = '%(asctime)s - %(levelname)s - %(message)s'
date_format = '%Y-%m-%d %H:%M:%S'
# Create a formatter
formatter = logging.Formatter(fmt=log_format, datefmt=date_format)
# Get the root logger
logger = logging.getLogger()
logger.setLevel(log_level)
# Check if the log file exists
if os.path.exists('my_log_file.log'):
# If the log file exists, create a handler for the log file
file_handler = logging.FileHandler(filename='my_log_file.log', mode='a')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
else:
# If the log file does not exist, create a handler for the console
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
logger.info("this is a new line")
main() # Call the main function
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 think using logging for everything makes sense here and have the default log level be info. The output is metadata about the process, not the result of the process itself.
Yea, probably that's how it should be. I was thinking about having INFO to be default but in some cases it is just a lot of data in terminal. IMO, it would be great to allow user to specify verbosity level instead of just ERROR, WARNING, INFO, with default (verbosity 0 or 1) to be minimum required to understand that scanner is working and scanning projects (probably print now) and maximum (e.g. 3) where we print everything.
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 think using logging for everything makes sense here and have the default log level be info. The output is metadata about the process, not the result of the process itself.
Will there always be a logging file as part of the arguments? coz I was thinking if that file name is optional and not mandatory we can separate the logging as a filehandle for file logging and console handler for console printing
something like this
import os import logging def main(): log_level = getattr(logging, 'INFO', None) log_format = '%(asctime)s - %(levelname)s - %(message)s' date_format = '%Y-%m-%d %H:%M:%S' # Create a formatter formatter = logging.Formatter(fmt=log_format, datefmt=date_format) # Get the root logger logger = logging.getLogger() logger.setLevel(log_level) # Check if the log file exists if os.path.exists('my_log_file.log'): # If the log file exists, create a handler for the log file file_handler = logging.FileHandler(filename='my_log_file.log', mode='a') file_handler.setFormatter(formatter) logger.addHandler(file_handler) else: # If the log file does not exist, create a handler for the console console_handler = logging.StreamHandler() console_handler.setFormatter(formatter) logger.addHandler(console_handler) logger.info("this is a new line") main() # Call the main function
The log filename argument is optional. If it is set, we print in the file and if not the argument is None and it is simply console printing as far as I remember.
src/gcp_scanner/scanner.py
Outdated
@@ -26,6 +26,9 @@ | |||
from pathlib import Path | |||
from typing import List, Tuple, Dict, Optional, Union | |||
|
|||
# Configure the logging module | |||
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S') |
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.
We have it already here:
gcp_scanner/src/gcp_scanner/scanner.py
Line 529 in b8c75d6
logging.basicConfig(level=getattr(logging, args.log_level.upper(), None), |
Accepted |
src/gcp_scanner/scanner.py
Outdated
@@ -197,7 +197,9 @@ def crawl_loop(initial_sa_tuples: List[Tuple[str, Credentials, List[str]]], | |||
continue | |||
|
|||
project_id = project['projectId'] | |||
print(f'Inspecting project {project_id}') | |||
# print(f'Inspecting project {project_id}') |
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.
Don't leave old code commented out. It's in the history anyway.
Just noticed activity in this PR, sorry for not replying later. Like I mentioned in the comment, I left it to be |
Why can't we set the default level to info and use logging? |
In this case, it generates too much info and terminal is overloaded with
text...
…On Mon, Jul 17, 2023, 11:41 AM Calle Svensson ***@***.***> wrote:
Why can't we set the default level to info and use logging?
—
Reply to this email directly, view it on GitHub
<#235 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABUFDYWSN25GLQ3FBSMK4I3XQWBO3ANCNFSM6AAAAAAZ2YTA6E>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Doesn't that suggest that we should shift a lot of the info messages to debug messages? |
I'd just use several levels of verbosity as it is usually done in scanners.
Debug could be part of that (e.g. last level).
…On Mon, Jul 17, 2023, 12:36 PM Calle Svensson ***@***.***> wrote:
Doesn't that suggest that we should shift a lot of the info messages to
debug messages?
—
Reply to this email directly, view it on GitHub
<#235 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABUFDYXHLZNBD4LTNIUUIQ3XQWH4JANCNFSM6AAAAAAZ2YTA6E>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
I am closing this one due to inactivity. TL;DR: we need a logging system with multiple levels of verbosity. |
Description
I have replaced the
print()
function with thelogging.info()
function in thescanner.py
file. This change allows the program to output log messages with timestamps, which is useful for tracking the progress of long-running scans.Changes Made
print()
function withlogging.info()
inscanner.py
.Checklist
Related Issues
Additional Notes
This change should make it easier to track the progress of long-running scans. The log messages now include a timestamp, which can be helpful for understanding when each event occurred. I have not added any new tests or updated the documentation, as I believe these changes are self-explanatory and do not significantly alter the functionality of the program.