Files
stacks-puppet-node/blockstack_client/logger.py
Aaron Blankstein 93d07d68b3 This reverts us *all* the way back to 523cf405d7 -- this removes all changes to support the token file from this branch.
Revert "added example request and response for PUT /v1/wallet/keys/owner to api docs"

This reverts commit d52ee4b31e.

Revert "cutting down on the verbosity of logging outputs -- registrar now only prints 1 line on wakeups. storage drivers are concatenated into 1 line"

This reverts commit 87e3e7ab0d.

Revert "adding dropbox as a default storage driver to load, and switched default 'required' drivers to 'disk,dropbox'"

This reverts commit 9471b0a20a.

Revert "adding test case for issue 483, which *also* required fixing the app session schema to handle empty string methods a little bit more gracefully"

This reverts commit 32efc99d62.

Revert "bugfix for the address reencoding in get_zonefile -- checks to see if the address is an address before trying to reencode"

This reverts commit 1488013b93.

Revert "Merge branch 'rc-0.14.3' of github.com:blockstack/blockstack-core into rc-0.14.3"

This reverts commit f75ab67960, reversing
changes made to fe863bcd3c.

Revert "don't create the metadata dir"

This reverts commit fe863bcd3c.

Revert "make all metadata directories inside the critical section"

This reverts commit e66236abd2.

Revert "don't cast 'None' to string by accident"

This reverts commit c6250d5349.

Revert "force string"

This reverts commit e72d43d0be.

Revert "add unbound proxy variable"

This reverts commit 7f1f7e9731.

Revert "return raw zonefile"

This reverts commit 51e858428d.

Revert "force string"

This reverts commit 1ce371644f.

Revert "force string"

This reverts commit 5353cb1015.

Revert "require virtualchain rc-0.14.3 and jsontokens-py 0.0.4"

This reverts commit 346f042db7.

Revert "Merge branch 'rc-0.14.3' of https://github.com/blockstack/blockstack-core into rc-0.14.3"

This reverts commit 1fa1de3e54, reversing
changes made to 523cf405d7.
2017-07-10 14:59:23 -04:00

148 lines
3.9 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
"""
Blockstack-client
~~~~~
copyright: (c) 2014 by Halfmoon Labs, Inc.
copyright: (c) 2015 by Blockstack.org
This file is part of Blockstack-client.
Blockstack-client is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Blockstack-client is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Blockstack-client. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import itertools
import logging
import traceback
import uuid
import urllib2
import copy
import time
import shutil
import requests
from ConfigParser import SafeConfigParser
import virtualchain
from .constants import *
class NetworkLogFormatter( logging.Formatter ):
"""
Log formatter for network endpoints, such as Blockstack Portal
"""
level_names = {
logging.DEBUG: 'DEBUG',
logging.INFO: 'INFO',
logging.WARN: 'WARN',
logging.ERROR: 'ERROR',
logging.FATAL: 'FATAL'
}
def format(self, record):
msg = record.msg
if msg is None:
msg = ' '
data = {
'time': int(time.time()),
'level': NetworkLogFormatter.level_names.get(record.levelno, 'TRACE'),
'category': os.path.basename(record.pathname),
'message': record.msg,
}
return data
class NetworkLogHandler( logging.Handler ):
"""
Log handler for network endpoints, such as Blockstack Portal
"""
def config(self, url, authorization):
self.authorization = authorization
self.url = url
def emit(self, record):
log_entry = self.format(record)
headers = {
'Authorization': self.authorization
}
try:
requests.post(self.url, json=log_entry, headers=headers, timeout=1.0)
except Exception as e:
pass
def get_network_log_handler(api_password=None, name=None, scheme="http", host="localhost", port=LOG_NETWORK_PORT):
"""
Get a log handler to sending messages over the network.
"""
level = logging.CRITICAL
if DEBUG:
logging.disable(logging.NOTSET)
level = logging.DEBUG
if name is None:
name = "<unknown>"
if api_password is None:
api_password = get_secret("BLOCKSTACK_API_PASSWORD")
if api_password is None:
# extract...
p = SafeConfigParser()
try:
p.read(CONFIG_PATH)
except:
return None
try:
if p.has_section('blockstack-client'):
if p.get('blockstack-client', 'api_password') is not None:
api_password = p.get('blockstack_client', 'api_password')
except:
return None
if not api_password:
return None
url = "{}://{}:{}".format(scheme, host, port)
authorization = 'bearer {}'.format(api_password)
network = NetworkLogHandler()
network.config(url, authorization)
network.setLevel( level )
formatter = NetworkLogFormatter()
network.setFormatter(formatter)
network.propagate = False
return network
def get_logger(name="blockstack-client", debug=DEBUG):
logger = virtualchain.get_logger(name)
logger.setLevel(logging.DEBUG if debug else logging.INFO)
if not BLOCKSTACK_TEST:
network_logger = get_network_log_handler(name=name)
if network_logger:
logger.addHandler(network_logger)
return logger