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

File based Python logging config #1301

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions cartography/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import argparse
import getpass
import logging
import logging.config
import os
import sys
from typing import Optional
Expand Down Expand Up @@ -111,6 +111,14 @@ def _build_parser(self):
'See https://neo4j.com/docs/api/python-driver/4.4/api.html#database.'
),
)
parser.add_argument(
'--logging-config',
type=str,
default=None,
help=(
'Path to file containing Python logging configuration'
),
)
parser.add_argument(
'--selected-modules',
type=str,
Expand Down Expand Up @@ -575,6 +583,9 @@ def main(self, argv: str) -> int:
# TODO support parameter lookup in environment variables if not present on command line
config: argparse.Namespace = self.parser.parse_args(argv)
# Logging config
if config.logging_config:
logging.config.fileConfig(config.logging_config)
logger.info("Using logging config from %s", config.logging_config)
if config.verbose:
logging.getLogger('cartography').setLevel(logging.DEBUG)
elif config.quiet:
Expand Down Expand Up @@ -802,9 +813,5 @@ def main(argv=None):
:rtype: int
:return: The return code.
"""
logging.basicConfig(level=logging.INFO)
logging.getLogger('botocore').setLevel(logging.WARNING)
logging.getLogger('googleapiclient').setLevel(logging.WARNING)
logging.getLogger('neo4j').setLevel(logging.WARNING)
argv = argv if argv is not None else sys.argv[1:]
sys.exit(CLI(prog='cartography').main(argv))
4 changes: 4 additions & 0 deletions cartography/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Config:
:param neo4j_database: The name of the database in Neo4j to connect to. If not specified, uses your Neo4j database
settings to infer which database is set to default.
See https://neo4j.com/docs/api/python-driver/4.4/api.html#database. Optional.
:type logging_config: str
:param logging_config: Path to the file containing logging configuration.
:type selected_modules: str
:param selected_modules: Comma-separated list of cartography top-level modules to sync. Optional.
:type update_tag: int
Expand Down Expand Up @@ -126,6 +128,7 @@ def __init__(
neo4j_password=None,
neo4j_max_connection_lifetime=None,
neo4j_database=None,
logging_config=None,
selected_modules=None,
update_tag=None,
aws_sync_all_profiles=False,
Expand Down Expand Up @@ -185,6 +188,7 @@ def __init__(
self.neo4j_password = neo4j_password
self.neo4j_max_connection_lifetime = neo4j_max_connection_lifetime
self.neo4j_database = neo4j_database
self.logging_config = logging_config
self.selected_modules = selected_modules
self.update_tag = update_tag
self.aws_sync_all_profiles = aws_sync_all_profiles
Expand Down
65 changes: 65 additions & 0 deletions logging.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[logger_root]
handlers=stdout,file
level=NOTSET

[logger_cartography.cli]
qualname=cartography.cli
handlers=stdout,file
# stop duplicate log massage
propagate=0
level=DEBUG

[logger_cartography.sync]
qualname=cartography.sync
handlers=stdout,file
# stop duplicate log massage
propagate=0

[logger_botocore]
qualname=botocore
level=WARNING
handlers=stdout,file

[logger_googleapiclient]
qualname=googleapiclient
level=WARNING
handlers=stdout,file

[logger_neo4j]
qualname=neo4j
handlers=stdout,file
level=WARNING

### Loggers ###
[loggers]
keys=root,cartography.cli,cartography.sync,neo4j,botocore,googleapiclient


### Formatters ###
[formatters]
keys=simple,detailed

[formatter_simple]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

[formatter_detailed]
format=%(asctime)s - %(name)s - %(levelname)s - %(module)s : %(lineno)d - %(message)s


### Handlers ###
[handlers]
keys=file,stdout

[handler_file]
class=handlers.TimedRotatingFileHandler
interval=midnight
backupCount=5
formatter=detailed
args=('logs/cartography.log',)
level=DEBUG

[handler_stdout]
class=StreamHandler
args=(sys.stdout,)
formatter=simple
level=INFO
Loading