controller.Log -> log.Log

This commit is contained in:
Aldo Cortesi
2016-10-19 14:14:59 +13:00
parent 83dbefb224
commit 966418725b
7 changed files with 51 additions and 51 deletions

View File

@@ -29,7 +29,7 @@ class WSGIApp:
**{"mitmproxy.master": ctx.master}
)
if err:
ctx.log.warn("Error in wsgi app. %s" % err, "error")
ctx.log.error("Error in wsgi app. %s" % err)
flow.reply.kill()
raise exceptions.AddonHalt()

View File

@@ -3,47 +3,6 @@ import queue
from mitmproxy import exceptions
class LogEntry:
def __init__(self, msg, level):
self.msg = msg
self.level = level
class Log:
"""
The central logger, exposed to scripts as mitmproxy.ctx.log.
"""
def __init__(self, master):
self.master = master
def debug(self, txt):
"""
Log with level debug.
"""
self(txt, "debug")
def info(self, txt):
"""
Log with level info.
"""
self(txt, "info")
def warn(self, txt):
"""
Log with level warn.
"""
self(txt, "warn")
def error(self, txt):
"""
Log with level error.
"""
self(txt, "error")
def __call__(self, text, level="info"):
self.master.add_log(text, level)
class Channel:
"""
The only way for the proxy server to communicate with the master

40
mitmproxy/log.py Normal file
View File

@@ -0,0 +1,40 @@
class LogEntry:
def __init__(self, msg, level):
self.msg = msg
self.level = level
class Log:
"""
The central logger, exposed to scripts as mitmproxy.ctx.log.
"""
def __init__(self, master):
self.master = master
def debug(self, txt):
"""
Log with level debug.
"""
self(txt, "debug")
def info(self, txt):
"""
Log with level info.
"""
self(txt, "info")
def warn(self, txt):
"""
Log with level warn.
"""
self(txt, "warn")
def error(self, txt):
"""
Log with level error.
"""
self(txt, "error")
def __call__(self, text, level="info"):
self.master.add_log(text, level)

View File

@@ -10,6 +10,7 @@ from mitmproxy import controller
from mitmproxy import events
from mitmproxy import exceptions
from mitmproxy import models
from mitmproxy import log
from mitmproxy.flow import io
from mitmproxy.protocol import http_replay
from netlib import basethread
@@ -50,7 +51,7 @@ class Master:
yield
return
mitmproxy_ctx.master = self
mitmproxy_ctx.log = controller.Log(self)
mitmproxy_ctx.log = log.Log(self)
try:
yield
finally:
@@ -66,7 +67,7 @@ class Master:
level: debug, info, warn, error
"""
with self.handlecontext():
self.addons("log", controller.LogEntry(e, level))
self.addons("log", log.LogEntry(e, level))
def start(self):
self.should_exit.clear()

View File

@@ -1,5 +1,5 @@
import netlib.exceptions
from mitmproxy import controller
from mitmproxy import log
from mitmproxy import exceptions
from mitmproxy import protocol
from mitmproxy.proxy import modes
@@ -113,7 +113,7 @@ class RootContext:
for i in subs:
full_msg.append(" -> " + i)
full_msg = "\n".join(full_msg)
self.channel.tell("log", controller.LogEntry(full_msg, level))
self.channel.tell("log", log.LogEntry(full_msg, level))
@property
def layers(self):

View File

@@ -5,7 +5,7 @@ import traceback
import netlib.exceptions
from mitmproxy import exceptions
from mitmproxy import models
from mitmproxy import controller
from mitmproxy import log
from mitmproxy.proxy import modes
from mitmproxy.proxy import root_context
from netlib import tcp
@@ -151,4 +151,4 @@ class ConnectionHandler:
def log(self, msg, level):
msg = "{}: {}".format(repr(self.client_conn.address), msg)
self.channel.tell("log", controller.LogEntry(msg, level))
self.channel.tell("log", log.LogEntry(msg, level))

View File

@@ -2,7 +2,7 @@ from .. import mastertest
import io
from mitmproxy.builtins import termlog
from mitmproxy import controller
from mitmproxy import log
from mitmproxy import dump
@@ -11,7 +11,7 @@ class TestTermLog(mastertest.MasterTest):
t = termlog.TermLog()
sio = io.StringIO()
t.configure(dump.Options(tfile = sio, verbosity = 2), set([]))
t.log(controller.LogEntry("one", "info"))
t.log(log.LogEntry("one", "info"))
assert "one" in sio.getvalue()
t.log(controller.LogEntry("two", "debug"))
t.log(log.LogEntry("two", "debug"))
assert "two" not in sio.getvalue()