Merge pull request #3 from racker/start_using_structlog

Add Structlog
This commit is contained in:
Paul Querna
2013年09月23日 20:11:45 -07:00

19
main.py
View File

@@ -17,13 +17,18 @@ limitations under the License.
"""
import sys
import twisted
from twisted.internet import reactor
from twisted.python import log
import structlog
from teeth_agent.logging import configure as configureLogging
from teeth_agent.agent import StandbyAgent
log.startLogging(sys.stdout)
agent = StandbyAgent([['localhost', 8081]])
agent.start()
reactor.run()
if __name__ == "__main__":
configureLogging()
structlog.twisted.plainJSONStdOutLogger
twisted.python.log.startLogging(sys.stderr)
agent = StandbyAgent([['localhost', 8081]])
agent.start()
reactor.run()

View File

@@ -4,3 +4,4 @@ distribute==0.6.24
simplejson==3.3.0
wsgiref==0.1.2
zope.interface==4.0.5
structlog==0.3.0

View File

@@ -15,7 +15,8 @@ limitations under the License.
"""
from teeth_agent.client import TeethClient
from twisted.python import log
from teeth_agent.logging import get_logger
log = get_logger()
class StandbyAgent(TeethClient):
@@ -26,6 +27,7 @@ class StandbyAgent(TeethClient):
def __init__(self, addrs):
super(StandbyAgent, self).__init__(addrs)
self._addHandler('v1', 'prepare_image', self.prepare_image)
log.info('Starting agent', addrs=addrs)
def prepare_image(self, image_id):
"""Prepare an Image."""

View File

@@ -19,9 +19,10 @@ import simplejson as json
from teeth_agent.protocol import TeethAgentProtocol
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.python.failure import Failure
from twisted.python import log
from twisted.internet import reactor
from twisted.internet.defer import maybeDeferred
from teeth_agent.logging import get_logger
log = get_logger()
__all__ = ["TeethClientFactory", "TeethClient"]

42
teeth_agent/logging.py Normal file
View File

@@ -0,0 +1,42 @@
"""
Copyright 2013 Rackspace, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import structlog
CONFIGURED_LOGGING = False
def configure():
"""
Configure logging subsystem.
"""
global CONFIGURED_LOGGING
if CONFIGURED_LOGGING:
return
CONFIGURED_LOGGING = True
structlog.configure(
context_class=dict,
cache_logger_on_first_use=True)
def get_logger():
"""
Get a logger instance.
"""
return structlog.get_logger()

View File

@@ -21,9 +21,11 @@ from twisted.internet import defer
from twisted.internet import reactor
from twisted.protocols.basic import LineReceiver
from twisted.python.failure import Failure
from twisted.python import log
from teeth_agent import __version__ as AGENT_VERSION
from teeth_agent.events import EventEmitter
from teeth_agent.logging import get_logger
log = get_logger()
DEFAULT_PROTOCOL_VERSION = 'v1'
@@ -92,15 +94,18 @@ class RPCProtocol(LineReceiver, EventEmitter):
self.address = address
self._pending_command_deferreds = {}
self._fatal_error = False
self._log = log.bind(host=address.host, port=address.port)
def loseConnectionSoon(self, timeout=10):
"""Attempt to disconnect from the transport as 'nicely' as possible. """
self._log.info('Trying to disconnect.')
self.transport.loseConnection()
reactor.callLater(timeout, self.transport.abortConnection)
def connectionMade(self):
"""TCP hard. We made it. Maybe."""
super(RPCProtocol, self).connectionMade()
self._log.info('Connection established.')
self.transport.setTcpKeepAlive(True)
self.transport.setTcpNoDelay(True)
self.emit('connect')
@@ -112,6 +117,8 @@ class RPCProtocol(LineReceiver, EventEmitter):
if not line:
return
self._log.debug('Got Line', line=line)
try:
message = json.loads(line)
except Exception:
@@ -148,6 +155,7 @@ class RPCProtocol(LineReceiver, EventEmitter):
def fatal_error(self, message):
"""Send a fatal error message, and disconnect."""
self._log.error('got a fatal error', message=message)
if not self._fatal_error:
self._fatal_error = True
self.sendLine(self.encoder.encode({
@@ -219,8 +227,7 @@ class TeethAgentProtocol(RPCProtocol):
def _on_connect(self, event):
def _response(result):
log.msg(format='Handshake successful, connection ID is %(connection_id)s',
connection_id=result['id'])
self._log.msg('Handshake successful', connection_id=result['id'])
return self.send_command('handshake',
{'id': 'a:b:c:d', 'version': AGENT_VERSION}).addCallback(_response)
{'id': 'a:b:c:d', 'version': AGENT_VERSION}).addCallback(_response)
Reference in New Issue
openstack/ironic-python-agent
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.

The note is not visible to the blocked user.