forked from LBRYCommunity/lbry-sdk
remove loggly
This commit is contained in:
parent
a30f3c86c2
commit
a056cd78f7
4 changed files with 0 additions and 125 deletions
|
@ -14,7 +14,6 @@ from aiohttp.web import GracefulExit
|
|||
from docopt import docopt
|
||||
|
||||
from lbry import __version__ as lbrynet_version
|
||||
from lbry.extras.daemon.loggly_handler import get_loggly_handler
|
||||
from lbry.extras.daemon.daemon import Daemon
|
||||
from lbry.conf import Config, CLIConfig
|
||||
|
||||
|
@ -255,10 +254,6 @@ def setup_logging(logger: logging.Logger, args: argparse.Namespace, conf: Config
|
|||
else:
|
||||
logger.getChild('lbry').setLevel(logging.DEBUG)
|
||||
|
||||
loggly_handler = get_loggly_handler(conf)
|
||||
loggly_handler.setLevel(logging.ERROR)
|
||||
logger.getChild('lbry').addHandler(loggly_handler)
|
||||
|
||||
|
||||
def run_daemon(args: argparse.Namespace, conf: Config):
|
||||
loop = asyncio.get_event_loop()
|
||||
|
|
|
@ -1,95 +0,0 @@
|
|||
import asyncio
|
||||
import json
|
||||
import logging.handlers
|
||||
import traceback
|
||||
|
||||
import typing
|
||||
from aiohttp.client_exceptions import ClientError
|
||||
import aiohttp
|
||||
from lbry import utils, __version__
|
||||
if typing.TYPE_CHECKING:
|
||||
from lbry.conf import Config
|
||||
|
||||
LOGGLY_TOKEN = 'BQEzZmMzLJHgAGxkBF00LGD0YGuyATVgAmqxAQEuAQZ2BQH4'
|
||||
|
||||
|
||||
class JsonFormatter(logging.Formatter):
|
||||
"""Format log records using json serialization"""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__()
|
||||
self.attributes = kwargs
|
||||
|
||||
def format(self, record):
|
||||
data = {
|
||||
'loggerName': record.name,
|
||||
'asciTime': self.formatTime(record),
|
||||
'fileName': record.filename,
|
||||
'functionName': record.funcName,
|
||||
'levelNo': record.levelno,
|
||||
'lineNo': record.lineno,
|
||||
'levelName': record.levelname,
|
||||
'message': record.getMessage(),
|
||||
}
|
||||
data.update(self.attributes)
|
||||
if record.exc_info:
|
||||
data['exc_info'] = self.formatException(record.exc_info)
|
||||
return json.dumps(data)
|
||||
|
||||
|
||||
class HTTPSLogglyHandler(logging.Handler):
|
||||
def __init__(self, loggly_token: str, config: 'Config'):
|
||||
super().__init__()
|
||||
self.cookies = {}
|
||||
self.url = "https://logs-01.loggly.com/inputs/{token}/tag/{tag}".format(
|
||||
token=utils.deobfuscate(loggly_token), tag='lbrynet-' + __version__
|
||||
)
|
||||
self._loop = asyncio.get_event_loop()
|
||||
self._session = aiohttp.ClientSession()
|
||||
self._config = config
|
||||
|
||||
@property
|
||||
def enabled(self):
|
||||
return self._config.share_usage_data
|
||||
|
||||
@staticmethod
|
||||
def get_full_message(record):
|
||||
if record.exc_info:
|
||||
return '\n'.join(traceback.format_exception(*record.exc_info))
|
||||
else:
|
||||
return record.getMessage()
|
||||
|
||||
async def _emit(self, record, retry=True):
|
||||
data = self.format(record).encode()
|
||||
try:
|
||||
async with self._session.post(self.url, data=data,
|
||||
cookies=self.cookies) as response:
|
||||
self.cookies.update(response.cookies)
|
||||
except ClientError:
|
||||
if self._loop.is_running() and retry and self.enabled:
|
||||
await self._session.close()
|
||||
self._session = aiohttp.ClientSession()
|
||||
return await self._emit(record, retry=False)
|
||||
|
||||
def emit(self, record):
|
||||
if not self.enabled:
|
||||
return
|
||||
try:
|
||||
asyncio.ensure_future(self._emit(record), loop=self._loop)
|
||||
except RuntimeError: # TODO: use a second loop
|
||||
print(f"\nfailed to send traceback to loggly, please file an issue with the following traceback:\n"
|
||||
f"{self.format(record)}")
|
||||
|
||||
def close(self):
|
||||
super().close()
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(self._session.close())
|
||||
except RuntimeError:
|
||||
pass
|
||||
|
||||
|
||||
def get_loggly_handler(config):
|
||||
handler = HTTPSLogglyHandler(LOGGLY_TOKEN, config=config)
|
||||
handler.setFormatter(JsonFormatter())
|
||||
return handler
|
|
@ -1,4 +1,3 @@
|
|||
from lbry.extras.daemon.loggly_handler import get_loggly_handler
|
||||
from lbry.testcase import CommandTestCase
|
||||
|
||||
|
||||
|
@ -29,14 +28,9 @@ class SettingsManagement(CommandTestCase):
|
|||
# test_privacy_settings (merged for reducing test time, unmerge when its fast)
|
||||
# tests that changing share_usage_data propagates to the relevant properties
|
||||
self.assertFalse(self.daemon.jsonrpc_settings_get()['share_usage_data'])
|
||||
loggly = get_loggly_handler(self.daemon.conf)
|
||||
self.addCleanup(loggly.close)
|
||||
self.assertFalse(self.daemon.analytics_manager.enabled)
|
||||
self.assertFalse(loggly.enabled)
|
||||
self.daemon.jsonrpc_settings_set('share_usage_data', True)
|
||||
self.assertTrue(self.daemon.jsonrpc_settings_get()['share_usage_data'])
|
||||
self.assertTrue(self.daemon.analytics_manager.enabled)
|
||||
self.assertTrue(loggly.enabled)
|
||||
self.daemon.jsonrpc_settings_set('share_usage_data', False)
|
||||
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@ from lbry.testcase import AsyncioTestCase
|
|||
from lbry.extras.cli import normalize_value, main, setup_logging
|
||||
from lbry.extras.system_info import get_platform
|
||||
from lbry.extras.daemon.daemon import Daemon
|
||||
from lbry.extras.daemon.loggly_handler import HTTPSLogglyHandler
|
||||
from lbry.conf import Config
|
||||
from lbry.extras import cli
|
||||
|
||||
|
@ -80,24 +79,6 @@ class CLILoggingTest(AsyncioTestCase):
|
|||
self.assertFalse(log.getChild("lbry").isEnabledFor(logging.DEBUG))
|
||||
self.assertFalse(log.getChild("torba").isEnabledFor(logging.DEBUG))
|
||||
|
||||
async def test_loggly(self):
|
||||
async with get_logger(["start"]) as log: # default share_usage_data=False
|
||||
log = log.getChild("lbry")
|
||||
self.assertIsInstance(log.handlers[0], logging.StreamHandler)
|
||||
self.assertIsInstance(log.handlers[1], logging.StreamHandler)
|
||||
self.assertIsInstance(log.handlers[2], HTTPSLogglyHandler)
|
||||
self.assertFalse(log.handlers[2].enabled)
|
||||
async with get_logger(["start"], share_usage_data=True) as log:
|
||||
log = log.getChild("lbry")
|
||||
self.assertEqual(len(log.handlers), 3)
|
||||
self.assertIsInstance(log.handlers[2], HTTPSLogglyHandler)
|
||||
self.assertTrue(log.handlers[2].enabled)
|
||||
async with get_logger(["start"], share_usage_data=False) as log: # explicit share_usage_data=False
|
||||
log = log.getChild("lbry")
|
||||
self.assertEqual(len(log.handlers), 3)
|
||||
self.assertIsInstance(log.handlers[2], HTTPSLogglyHandler)
|
||||
self.assertFalse(log.handlers[2].enabled)
|
||||
|
||||
async def test_quiet(self):
|
||||
async with get_logger(["start"]) as log: # default is loud
|
||||
log = log.getChild("lbry")
|
||||
|
|
Loading…
Reference in a new issue