Table Of Contents

Previous topic

Examples for rrlog

Next topic

Examples for rrlog

This Page

Examples for rrlog

A Remote Rotating Log for Python that works instantly

Remote with sockets

This is the preferred way to log remotely. It is also the right way for processes logging to the same file (with a server remotely, or on the same machine).

Host and Ports

By default, the connection uses “localhost” and a default port → rrlog.globalconst.DEFAULTPORT_SOCKET

You can specify a list of allowed ports on both sides. The server listens on the first port available. The client logs to the first port where a server is found.

A socket client in your application

from rrlog import socketclient

# with no ports/host given, default port and "localhost" are used
log = socketclient.createClientLog(
	errorHandler="stderr",
	ports = (9801,9802,9803,) # use the first port where a server is available
	)

# now, log some lines:
for i in xrange(31):
	log("Socket-client line %s"%(i))

rrlog.socketclient

(Re-)Connection behavior

A log client uses the log server whenever available. While the log server is down, messages are lost silently. When the server is up again, all clients start to use the server again.

Common server examples

For the socket client to make sense, start a log server in another process:

A socket server that prints to stdout

# First, create a pure print server:
# (this is independent from the remote connection type)
from rrlog.server import printwriter
srv = printwriter.createServer()

# Start the server as a socket server:
from rrlog.server import socketserver

socketserver.startServer(
	srv,
	ports=(9801, 9802, 9803), # try in this order, use the first port available
	)
# The server waits for requests now.

rrlog.server.socketserver

rrlog.server.printwriter

A socket server that logs into files

# First, create a pure file-writing server:
# (this is independent from the connection type, like XMLRPC)
from rrlog.server import filewriter

logServer = filewriter.createRotatingServer(
	filePathPattern = "./demo-log-%s.txt", # "pattern" because %s (or %d) is required for the rotate-number
	rotateCount=3,
	rotateLineMin=10,
	)

# Start the server as an XMLRPC server:
from rrlog.server import socketserver

# with no ports/host given, default port and "localhost" are used
socketserver.startServer(
	logServer,
	ports=(9801, 9802, 9803), # try in this order, use the first port available
	)
# The server waits for requests now.

rrlog.server.socketserver

rrlog.server.filewriter

A socket server that logs into a database

# First, create a pure database-writing server:
# (this is independent from the remote connection protocol)
from rrlog.server import dbwriter_sa
engineStr = "mysql://logtester@localhost/logtest"

logServer = dbwriter_sa.createRotatingServer(
	engineStr = engineStr,
	tableNamePattern = "logtable_%s",  # "pattern" because %s (or %d) is required for the rotate-number
	rotateCount=3,
	rotateLineMin=10,
    tsFormat="std1", # Timestamp format: std1 is shorthand for the strftime-format "%H:%M.%S;%3N"
	)

# Start the server as a pure socket server:
from rrlog.server import socketserver

socketserver.startServer(
	logServer,
	ports=(9801, 9802, 9803), # try in this order, use the first port available
	)
# The server waits for requests now.

rrlog.server.socketserver

rrlog.server.dbwriter_sa