Skip to content

Commit

Permalink
Add support fro python logging module (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz.walkiewicz authored Jun 3, 2019
1 parent f5b8d6b commit f4666bd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions neptune/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def create_experiment(name=None,
tags=None,
upload_source_files=None,
abort_callback=None,
logger=None,
upload_stdout=True,
upload_stderr=True,
send_hardware_metrics=True,
Expand All @@ -90,6 +91,7 @@ def create_experiment(name=None,
tags=tags,
upload_source_files=upload_source_files,
abort_callback=abort_callback,
logger=logger,
upload_stdout=upload_stdout,
upload_stderr=upload_stderr,
send_hardware_metrics=send_hardware_metrics,
Expand Down
2 changes: 2 additions & 0 deletions neptune/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ def get_numeric_channels_values(self, *channel_names):
def start(self,
upload_source_files=None,
abort_callback=None,
logger=None,
upload_stdout=True,
upload_stderr=True,
send_hardware_metrics=True,
Expand All @@ -564,6 +565,7 @@ def start(self,

self._execution_context.start(
abort_callback=abort_callback,
logger=logger,
upload_stdout=upload_stdout,
upload_stderr=upload_stderr,
send_hardware_metrics=send_hardware_metrics,
Expand Down
17 changes: 17 additions & 0 deletions neptune/internal/execution/execution_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
import sys
import time
import traceback
from logging import StreamHandler

from neptune.internal.abort import DefaultAbortImpl, CustomAbortImpl
from neptune.internal.channels.channels import ChannelNamespace
from neptune.internal.hardware.gauges.gauge_mode import GaugeMode
from neptune.internal.hardware.metrics.service.metric_service_factory import MetricServiceFactory
from neptune.internal.hardware.system.system_monitor import SystemMonitor
from neptune.internal.streams.channel_writer import ChannelWriter
from neptune.internal.streams.stdstream_uploader import StdOutWithUpload, StdErrWithUpload
from neptune.internal.threads.aborting_thread import AbortingThread
from neptune.internal.threads.hardware_metric_reporting_thread import HardwareMetricReportingThread
Expand All @@ -38,6 +41,8 @@ def __init__(self, client, experiment):
self._ping_thread = None
self._hardware_metric_thread = None
self._aborting_thread = None
self._logger = None
self._logger_handler = None
self._stdout_uploader = None
self._stderr_uploader = None
self._uncaught_exception_handler = sys.__excepthook__
Expand All @@ -46,6 +51,7 @@ def __init__(self, client, experiment):

def start(self,
abort_callback=None,
logger=None,
upload_stdout=True,
upload_stderr=True,
send_hardware_metrics=True,
Expand All @@ -60,6 +66,14 @@ def start(self,
if abortable:
self._run_aborting_thread(abort_callback)

if logger:
# pylint: disable=protected-access
channel = self._experiment._get_channel('logger', 'text', ChannelNamespace.SYSTEM)
channel_writer = ChannelWriter(self._experiment, channel.name, ChannelNamespace.SYSTEM)
self._logger_handler = StreamHandler(channel_writer)
self._logger = logger
logger.addHandler(self._logger_handler)

if upload_stdout and not is_notebook():
self._stdout_uploader = StdOutWithUpload(self._experiment)

Expand Down Expand Up @@ -91,6 +105,9 @@ def stop(self):
if self._stderr_uploader:
self._stderr_uploader.close()

if self._logger and self._logger_handler:
self._logger.removeHandler(self._logger_handler)

sys.excepthook = self._previous_uncaught_exception_handler

def _set_uncaught_exception_handler(self):
Expand Down
3 changes: 2 additions & 1 deletion neptune/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import os

import click

import pandas as pd

from neptune.envs import NOTEBOOK_ID_ENV_NAME
Expand Down Expand Up @@ -224,6 +223,7 @@ def create_experiment(self,
tags=None,
upload_source_files=None,
abort_callback=None,
logger=None,
upload_stdout=True,
upload_stderr=True,
send_hardware_metrics=True,
Expand Down Expand Up @@ -281,6 +281,7 @@ def create_experiment(self,
experiment.start(
upload_source_files=upload_source_files,
abort_callback=abort_callback,
logger=logger,
upload_stdout=upload_stdout,
upload_stderr=upload_stderr,
send_hardware_metrics=send_hardware_metrics,
Expand Down

0 comments on commit f4666bd

Please sign in to comment.