Skip to content

Commit

Permalink
change --write-log behaviour
Browse files Browse the repository at this point in the history
- log files now get truncated when opening them
  (mode "w" instead of "a")
- log verbosity to file depends on -q/-v
  (same  as logging to stderr)
  • Loading branch information
mikf committed Jan 26, 2018
1 parent 97f4f15 commit c9a9664
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions gallery_dl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@
log = logging.getLogger("gallery-dl")


def initialize_logging():
def initialize_logging(loglevel, formatter):
# convert levelnames to lowercase
for level in (10, 20, 30, 40, 50):
name = logging.getLevelName(level)
logging.addLevelName(level, name.lower())
# setup basic logging to stderr
formatter = logging.Formatter("[%(name)s][%(levelname)s] %(message)s")
handler = logging.StreamHandler()
handler.setFormatter(formatter)
root = logging.getLogger()
root.setLevel(logging.INFO)
root.setLevel(loglevel)
root.addHandler(handler)


Expand Down Expand Up @@ -78,10 +77,11 @@ def prepare_filter(filterexpr, target):

def main():
try:
initialize_logging()
parser = option.build_parser()
args = parser.parse_args()
logging.getLogger().setLevel(args.loglevel)

formatter = logging.Formatter("[%(name)s][%(levelname)s] %(message)s")
initialize_logging(args.loglevel, formatter)

# configuration
if args.load_config:
Expand All @@ -94,7 +94,19 @@ def main():
config.set(key, value)
config.set(("_",), {})

# logging
# logfile
logfile = config.interpolate(("output", "logfile"))
if logfile:
try:
path = util.expand_path(logfile)
handler = logging.FileHandler(path, "w")
except OSError as exc:
log.warning("logfile: %s", exc)
else:
handler.setFormatter(formatter)
logging.getLogger().addHandler(handler)

# loglevels
if args.loglevel >= logging.ERROR:
config.set(("output", "mode"), "null")
elif args.loglevel <= logging.DEBUG:
Expand All @@ -108,21 +120,6 @@ def main():
except AttributeError:
pass

# logfile
logfile = config.interpolate(("output", "logfile"))
if logfile:
try:
handler = logging.FileHandler(util.expand_path(logfile))
except OSError as exc:
log.warning("logfile: %s", exc)
else:
formatter = logging.Formatter(
"[%(asctime)s][%(name)s][%(levelname)s] %(message)s",
"%Y-%m-%d %H:%M:%S")
handler.setFormatter(formatter)
handler.setLevel(logging.INFO)
logging.getLogger().addHandler(handler)

if args.list_modules:
for module_name in extractor.modules:
print(module_name)
Expand Down Expand Up @@ -160,6 +157,7 @@ def main():
file = sys.stdin
else:
file = open(args.inputfile)

urls += sanatize_input(file)
except OSError as exc:
log.warning("input-file: %s", exc)
Expand Down

0 comments on commit c9a9664

Please sign in to comment.