twisted.python.logfile.LogFile

Here are the examples of the python api twisted.python.logfile.LogFile taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

24 Examples 7

Example 1

Project: canvas Source File: server.py
def CanvasApp(nginx=False):
    application = service.Application("Canvas")

    web_server = CanvasTCPServer()
    web_server.setServiceParent(application)
    
    
    log_file = logfile.LogFile(
        name="twistd.log", 
        directory="/var/canvas/website/run",
        maxRotatedFiles=100,
    )
    
    application.setComponent(log.ILogObserver, log.FileLogObserver(log_file).emit)

    return application

Example 2

Project: drawquest-web Source File: server.py
def CanvasApp(nginx=False):
    application = service.Application("Canvas")

    web_server = CanvasTCPServer()
    web_server.setServiceParent(application)


    log_file = logfile.LogFile(
        name="twistd.log",
        directory="/var/canvas/website/run",
        maxRotatedFiles=100,
    )

    application.setComponent(log.ILogObserver, log.FileLogObserver(log_file).emit)

    return application

Example 3

Project: mythbox Source File: test_logfile.py
Function: test_writing
    def testWriting(self):
        log = logfile.LogFile(self.name, self.dir)
        log.write("123")
        log.write("456")
        log.flush()
        log.write("7890")
        log.close()

        f = open(self.path, "r")
        self.assertEquals(f.read(), "1234567890")
        f.close()

Example 4

Project: mythbox Source File: test_logfile.py
Function: test_append
    def testAppend(self):
        log = logfile.LogFile(self.name, self.dir)
        log.write("0123456789")
        log.close()

        log = logfile.LogFile(self.name, self.dir)
        self.assertEquals(log.size, 10)
        self.assertEquals(log._file.tell(), log.size)
        log.write("abc")
        self.assertEquals(log.size, 13)
        self.assertEquals(log._file.tell(), log.size)
        f = log._file
        f.seek(0, 0)
        self.assertEquals(f.read(), "0123456789abc")
        log.close()

Example 5

Project: mythbox Source File: test_logfile.py
    def testModePreservation(self):
        """
        Check rotated files have same permissions as original.
        """
        f = open(self.path, "w").close()
        os.chmod(self.path, 0707)
        mode = os.stat(self.path)[stat.ST_MODE]
        log = logfile.LogFile(self.name, self.dir)
        log.write("abc")
        log.rotate()
        self.assertEquals(mode, os.stat(self.path)[stat.ST_MODE])

Example 6

Project: mythbox Source File: test_logfile.py
    def test_defaultPermissions(self):
        """
        Test the default permission of the log file: if the file exist, it
        should keep the permission.
        """
        f = file(self.path, "w")
        os.chmod(self.path, 0707)
        currentMode = stat.S_IMODE(os.stat(self.path)[stat.ST_MODE])
        f.close()
        log1 = logfile.LogFile(self.name, self.dir)
        self.assertEquals(stat.S_IMODE(os.stat(self.path)[stat.ST_MODE]),
                          currentMode)

Example 7

Project: mythbox Source File: test_logfile.py
Function: test_reopen
    def test_reopen(self):
        """
        L{logfile.LogFile.reopen} allows to rename the currently used file and
        make L{logfile.LogFile} create a new file.
        """
        log1 = logfile.LogFile(self.name, self.dir)
        log1.write("hello1")
        savePath = os.path.join(self.dir, "save.log")
        os.rename(self.path, savePath)
        log1.reopen()
        log1.write("hello2")
        log1.close()

        f = open(self.path, "r")
        self.assertEquals(f.read(), "hello2")
        f.close()
        f = open(savePath, "r")
        self.assertEquals(f.read(), "hello1")
        f.close()

Example 8

Project: SubliminalCollaborator Source File: test_logfile.py
Function: test_writing
    def testWriting(self):
        log = logfile.LogFile(self.name, self.dir)
        log.write("123")
        log.write("456")
        log.flush()
        log.write("7890")
        log.close()

        f = open(self.path, "r")
        self.assertEqual(f.read(), "1234567890")
        f.close()

Example 9

Project: SubliminalCollaborator Source File: test_logfile.py
Function: test_append
    def testAppend(self):
        log = logfile.LogFile(self.name, self.dir)
        log.write("0123456789")
        log.close()

        log = logfile.LogFile(self.name, self.dir)
        self.assertEqual(log.size, 10)
        self.assertEqual(log._file.tell(), log.size)
        log.write("abc")
        self.assertEqual(log.size, 13)
        self.assertEqual(log._file.tell(), log.size)
        f = log._file
        f.seek(0, 0)
        self.assertEqual(f.read(), "0123456789abc")
        log.close()

Example 10

Project: SubliminalCollaborator Source File: test_logfile.py
    def testModePreservation(self):
        """
        Check rotated files have same permissions as original.
        """
        f = open(self.path, "w").close()
        os.chmod(self.path, 0707)
        mode = os.stat(self.path)[stat.ST_MODE]
        log = logfile.LogFile(self.name, self.dir)
        log.write("abc")
        log.rotate()
        self.assertEqual(mode, os.stat(self.path)[stat.ST_MODE])

Example 11

Project: SubliminalCollaborator Source File: test_logfile.py
    def test_defaultPermissions(self):
        """
        Test the default permission of the log file: if the file exist, it
        should keep the permission.
        """
        f = file(self.path, "w")
        os.chmod(self.path, 0707)
        currentMode = stat.S_IMODE(os.stat(self.path)[stat.ST_MODE])
        f.close()
        log1 = logfile.LogFile(self.name, self.dir)
        self.assertEqual(stat.S_IMODE(os.stat(self.path)[stat.ST_MODE]),
                          currentMode)

Example 12

Project: SubliminalCollaborator Source File: test_logfile.py
Function: test_reopen
    def test_reopen(self):
        """
        L{logfile.LogFile.reopen} allows to rename the currently used file and
        make L{logfile.LogFile} create a new file.
        """
        log1 = logfile.LogFile(self.name, self.dir)
        log1.write("hello1")
        savePath = os.path.join(self.dir, "save.log")
        os.rename(self.path, savePath)
        log1.reopen()
        log1.write("hello2")
        log1.close()

        f = open(self.path, "r")
        self.assertEqual(f.read(), "hello2")
        f.close()
        f = open(savePath, "r")
        self.assertEqual(f.read(), "hello1")
        f.close()

Example 13

Project: SubliminalCollaborator Source File: test_logfile.py
Function: test_nonexistent_dir
    def test_nonExistentDir(self):
        """
        Specifying an invalid directory to L{LogFile} raises C{IOError}.
        """
        e = self.assertRaises(
            IOError, logfile.LogFile, self.name, 'this_dir_does_not_exist')
        self.assertEqual(e.errno, errno.ENOENT)

Example 14

Project: tahoe-lafs Source File: tahoesvc.py
    def launch_node(self):
        try:
            logmsg("main thread startup")

            # import dependencies so that py2exe finds them
            # nevow requires all these for its voodoo module import time adaptor registrations
            from nevow import accessors, appserver, static, rend, url, util, query, i18n, flat
            from nevow import guard, stan, testutil, context
            from nevow.flat import flatmdom, flatstan, twist
            from formless import webform, processors, annotate, iformless
            from decimal import Decimal

            import allmydata.web

            # junk to appease pyflakes's outrage at py2exe's needs
            [
                accessors, appserver, static, rend, url, util, query, i18n, flat, guard, stan, testutil,
                context, flatmdom, flatstan, twist, webform, processors, annotate, iformless, Decimal,
                allmydata,
            ]

            from twisted.internet import reactor
            from twisted.python import log, logfile
            from allmydata import client

            # set up twisted logging. this will become part of the node rsn.
            logdir = os.path.join(basedir, 'logs')
            if not os.path.exists(logdir):
                os.makedirs(logdir)
            lf = logfile.LogFile('tahoesvc.log', logdir)
            log.startLogging(lf)

            # run the node itself
            c = client.Client(basedir)
            reactor.callLater(0, c.startService) # after reactor startup
            reactor.run(installSignalHandlers=False)

            logmsg("main thread shutdown")
        except:
            logmsg("exception")
            traceback.print_exc(None, logfilehandle)
            logfilehandle.flush()
            os.abort()

Example 15

Project: mythbox Source File: test_logfile.py
Function: test_rotation
    def testRotation(self):
        # this logfile should rotate every 10 bytes
        log = logfile.LogFile(self.name, self.dir, rotateLength=10)

        # test automatic rotation
        log.write("123")
        log.write("4567890")
        log.write("1" * 11)
        self.assert_(os.path.exists("%s.1" % self.path))
        self.assert_(not os.path.exists("%s.2" % self.path))
        log.write('')
        self.assert_(os.path.exists("%s.1" % self.path))
        self.assert_(os.path.exists("%s.2" % self.path))
        self.assert_(not os.path.exists("%s.3" % self.path))
        log.write("3")
        self.assert_(not os.path.exists("%s.3" % self.path))

        # test manual rotation
        log.rotate()
        self.assert_(os.path.exists("%s.3" % self.path))
        self.assert_(not os.path.exists("%s.4" % self.path))
        log.close()

        self.assertEquals(log.listLogs(), [1, 2, 3])

Example 16

Project: mythbox Source File: test_logfile.py
Function: testlogreader
    def testLogReader(self):
        log = logfile.LogFile(self.name, self.dir)
        log.write("abc\n")
        log.write("def\n")
        log.rotate()
        log.write("ghi\n")
        log.flush()

        # check reading logs
        self.assertEquals(log.listLogs(), [1])
        reader = log.getCurrentLog()
        reader._file.seek(0)
        self.assertEquals(reader.readLines(), ["ghi\n"])
        self.assertEquals(reader.readLines(), [])
        reader.close()
        reader = log.getLog(1)
        self.assertEquals(reader.readLines(), ["abc\n", "def\n"])
        self.assertEquals(reader.readLines(), [])
        reader.close()

        # check getting illegal log readers
        self.assertRaises(ValueError, log.getLog, 2)
        self.assertRaises(TypeError, log.getLog, "1")

        # check that log numbers are higher for older logs
        log.rotate()
        self.assertEquals(log.listLogs(), [1, 2])
        reader = log.getLog(1)
        reader._file.seek(0)
        self.assertEquals(reader.readLines(), ["ghi\n"])
        self.assertEquals(reader.readLines(), [])
        reader.close()
        reader = log.getLog(2)
        self.assertEquals(reader.readLines(), ["abc\n", "def\n"])
        self.assertEquals(reader.readLines(), [])
        reader.close()

Example 17

Project: mythbox Source File: test_logfile.py
Function: test_no_permission
    def test_noPermission(self):
        """
        Check it keeps working when permission on dir changes.
        """
        log = logfile.LogFile(self.name, self.dir)
        log.write("abc")

        # change permissions so rotation would fail
        os.chmod(self.dir, 0555)

        # if this succeeds, chmod doesn't restrict us, so we can't
        # do the test
        try:
            f = open(os.path.join(self.dir,"xxx"), "w")
        except (OSError, IOError):
            pass
        else:
            f.close()
            return

        log.rotate() # this should not fail

        log.write("def")
        log.flush()

        f = log._file
        self.assertEquals(f.tell(), 6)
        f.seek(0, 0)
        self.assertEquals(f.read(), "abcdef")
        log.close()

Example 18

Project: mythbox Source File: test_logfile.py
Function: test_maxnumberoflog
    def test_maxNumberOfLog(self):
        """
        Test it respect the limit on the number of files when maxRotatedFiles
        is not None.
        """
        log = logfile.LogFile(self.name, self.dir, rotateLength=10,
                              maxRotatedFiles=3)
        log.write("1" * 11)
        log.write("2" * 11)
        self.failUnless(os.path.exists("%s.1" % self.path))

        log.write("3" * 11)
        self.failUnless(os.path.exists("%s.2" % self.path))

        log.write("4" * 11)
        self.failUnless(os.path.exists("%s.3" % self.path))
        self.assertEquals(file("%s.3" % self.path).read(), "1" * 11)

        log.write("5" * 11)
        self.assertEquals(file("%s.3" % self.path).read(), "2" * 11)
        self.failUnless(not os.path.exists("%s.4" % self.path))

Example 19

Project: jolicloud-daemon Source File: main.py
def start():
    root = static.File(os.environ['JPD_HTDOCS_PATH'])
    root.processors = { '.rpy': script.ResourceScript }
    root = rewrite.RewriterResource(root, rewrite.alias('cgi-bin/get_icon.py', 'get_icon.rpy'))
    
    site = JolicloudWSSite(root)
    site.addHandler('/jolicloud/', JolicloudWSHandler)
    
    # Setting up the log file path
    if os.environ.get('JPD_SYSTEM', '0') == '1':
        if os.getuid():
            log.err('You must be root to run this daemon in system mode.')
            exit()
        log_path = '/var/log'
    else:
        try:
            import xdg.BaseDirectory
            log_path = xdg.BaseDirectory.save_data_path('Jolicloud', 'jolicloud-daemon')
        except ImportError:
            log_path = os.path.join(os.getenv('HOME'), '.local', 'share', 'Jolicloud', 'jolicloud-daemon')
    
    port = int(os.environ.get('JPD_PORT', 804 if os.environ.get('JPD_SYSTEM', None) else 8004))
    
    # http://twistedmatrix.com/docuements/9.0.0/web/howto/using-twistedweb.html#auto5
    if os.environ.get('JPD_DEBUG', '0') == '1':
        log.startLogging(sys.stdout)
        log.startLogging(LogFile('jolicloud-daemon.log', log_path, maxRotatedFiles=2))
        reactor.listenTCP(port, site)
    else:
        log.startLogging(LogFile('jolicloud-daemon.log', log_path, maxRotatedFiles=2))
        reactor.listenTCP(port, site, interface='127.0.0.1')
    # TODO, use random port for session daemon
    
    # We load the plugins:
    if os.environ.get('JPD_SYSTEM', '0') == '1':
        log.msg('We load the system plugins.')
        plugins = getPlugins(ijolidaemon.ISystemManager, managers)
    else:
        log.msg('We load the session plugins.')
        plugins = getPlugins(ijolidaemon.ISessionManager, managers)
    for plugin in plugins:
        log.msg(plugin.__class__.__name__)
    
    reactor.run()

Example 20

Project: SubliminalCollaborator Source File: test_logfile.py
Function: test_rotation
    def testRotation(self):
        # this logfile should rotate every 10 bytes
        log = logfile.LogFile(self.name, self.dir, rotateLength=10)

        # test automatic rotation
        log.write("123")
        log.write("4567890")
        log.write("1" * 11)
        self.assert_(os.path.exists("%s.1" % self.path))
        self.assert_(not os.path.exists("%s.2" % self.path))
        log.write('')
        self.assert_(os.path.exists("%s.1" % self.path))
        self.assert_(os.path.exists("%s.2" % self.path))
        self.assert_(not os.path.exists("%s.3" % self.path))
        log.write("3")
        self.assert_(not os.path.exists("%s.3" % self.path))

        # test manual rotation
        log.rotate()
        self.assert_(os.path.exists("%s.3" % self.path))
        self.assert_(not os.path.exists("%s.4" % self.path))
        log.close()

        self.assertEqual(log.listLogs(), [1, 2, 3])

Example 21

Project: SubliminalCollaborator Source File: test_logfile.py
Function: testlogreader
    def testLogReader(self):
        log = logfile.LogFile(self.name, self.dir)
        log.write("abc\n")
        log.write("def\n")
        log.rotate()
        log.write("ghi\n")
        log.flush()

        # check reading logs
        self.assertEqual(log.listLogs(), [1])
        reader = log.getCurrentLog()
        reader._file.seek(0)
        self.assertEqual(reader.readLines(), ["ghi\n"])
        self.assertEqual(reader.readLines(), [])
        reader.close()
        reader = log.getLog(1)
        self.assertEqual(reader.readLines(), ["abc\n", "def\n"])
        self.assertEqual(reader.readLines(), [])
        reader.close()

        # check getting illegal log readers
        self.assertRaises(ValueError, log.getLog, 2)
        self.assertRaises(TypeError, log.getLog, "1")

        # check that log numbers are higher for older logs
        log.rotate()
        self.assertEqual(log.listLogs(), [1, 2])
        reader = log.getLog(1)
        reader._file.seek(0)
        self.assertEqual(reader.readLines(), ["ghi\n"])
        self.assertEqual(reader.readLines(), [])
        reader.close()
        reader = log.getLog(2)
        self.assertEqual(reader.readLines(), ["abc\n", "def\n"])
        self.assertEqual(reader.readLines(), [])
        reader.close()

Example 22

Project: SubliminalCollaborator Source File: test_logfile.py
Function: test_no_permission
    def test_noPermission(self):
        """
        Check it keeps working when permission on dir changes.
        """
        log = logfile.LogFile(self.name, self.dir)
        log.write("abc")

        # change permissions so rotation would fail
        os.chmod(self.dir, 0555)

        # if this succeeds, chmod doesn't restrict us, so we can't
        # do the test
        try:
            f = open(os.path.join(self.dir,"xxx"), "w")
        except (OSError, IOError):
            pass
        else:
            f.close()
            return

        log.rotate() # this should not fail

        log.write("def")
        log.flush()

        f = log._file
        self.assertEqual(f.tell(), 6)
        f.seek(0, 0)
        self.assertEqual(f.read(), "abcdef")
        log.close()

Example 23

Project: SubliminalCollaborator Source File: test_logfile.py
Function: test_maxnumberoflog
    def test_maxNumberOfLog(self):
        """
        Test it respect the limit on the number of files when maxRotatedFiles
        is not None.
        """
        log = logfile.LogFile(self.name, self.dir, rotateLength=10,
                              maxRotatedFiles=3)
        log.write("1" * 11)
        log.write("2" * 11)
        self.failUnless(os.path.exists("%s.1" % self.path))

        log.write("3" * 11)
        self.failUnless(os.path.exists("%s.2" % self.path))

        log.write("4" * 11)
        self.failUnless(os.path.exists("%s.3" % self.path))
        self.assertEqual(file("%s.3" % self.path).read(), "1" * 11)

        log.write("5" * 11)
        self.assertEqual(file("%s.3" % self.path).read(), "2" * 11)
        self.failUnless(not os.path.exists("%s.4" % self.path))

Example 24

Project: SubliminalCollaborator Source File: server.py
Function: open_log_file
    def _openLogFile(self, path):
        from twisted.python import logfile
        return logfile.LogFile(os.path.basename(path), os.path.dirname(path))