本文整理匯總了Python中twisted.python.logfile.LogFile方法的典型用法代碼示例。如果您正苦於以下問題:Python logfile.LogFile方法的具體用法?Python logfile.LogFile怎麽用?Python logfile.LogFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類twisted.python.logfile
的用法示例。
在下文中一共展示了logfile.LogFile方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_append
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
def test_append(self):
"""
Log files can be written to, closed. Their size is the number of
bytes written to them. Everything that was written to them can
be read, even if the writing happened on separate occasions,
and even if the log file was closed in between.
"""
with contextlib.closing(logfile.LogFile(self.name, self.dir)) as log:
log.write("0123456789")
log = logfile.LogFile(self.name, self.dir)
self.addCleanup(log.close)
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")
示例2: test_maxNumberOfLog
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
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)
self.addCleanup(log.close)
log.write("1" * 11)
log.write("2" * 11)
self.assertTrue(os.path.exists("{0}.1".format(self.path)))
log.write("3" * 11)
self.assertTrue(os.path.exists("{0}.2".format(self.path)))
log.write("4" * 11)
self.assertTrue(os.path.exists("{0}.3".format(self.path)))
with open("{0}.3".format(self.path)) as fp:
self.assertEqual(fp.read(), "1" * 11)
log.write("5" * 11)
with open("{0}.3".format(self.path)) as fp:
self.assertEqual(fp.read(), "2" * 11)
self.assertFalse(os.path.exists("{0}.4".format(self.path)))
示例3: test_cantChangeFileMode
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
def test_cantChangeFileMode(self):
"""
Opening a L{LogFile} which can be read and write but whose mode can't
be changed doesn't trigger an error.
"""
if runtime.platform.isWindows():
name, directory = "NUL", ""
expectedPath = "NUL"
else:
name, directory = "null", "/dev"
expectedPath = "/dev/null"
log = logfile.LogFile(name, directory, defaultMode=0o555)
self.addCleanup(log.close)
self.assertEqual(log.path, expectedPath)
self.assertEqual(log.defaultMode, 0o555)
示例4: test_append
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
def test_append(self):
"""
Log files can be written to, closed. Their size is the number of
bytes written to them. Everything that was written to them can
be read, even if the writing happened on separate occasions,
and even if the log file was closed in between.
"""
with contextlib.closing(logfile.LogFile(self.name, self.dir)) as log:
log.write("0123456789")
log = logfile.LogFile(self.name, self.dir)
self.addCleanup(log.close)
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(), b"0123456789abc")
示例5: testRotation
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
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])
示例6: test_maxNumberOfLog
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
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))
示例7: testRotation
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
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])
示例8: CanvasApp
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
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
示例9: logger
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
def logger():
try:
if 'ANCHORE_LOGFILE' in os.environ:
thefile = os.environ['ANCHORE_LOGFILE']
else:
thefile = "anchore-general.log"
except:
thefile = "anchore-general.log"
f = logfile.LogFile(thefile, '/var/log/', rotateLength=10000000, maxRotatedFiles=10)
log_observer = log.FileLogObserver(f)
return log_observer.emit
#def logger():
# return log.PythonLoggingObserver().emit
示例10: _openLogFile
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
def _openLogFile(self, path):
from twisted.python import logfile
return logfile.LogFile(os.path.basename(path), os.path.dirname(path))
示例11: test_writing
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
def test_writing(self):
"""
Log files can be written to, flushed and closed. Closing a log file
also flushes it.
"""
with contextlib.closing(logfile.LogFile(self.name, self.dir)) as log:
log.write("123")
log.write("456")
log.flush()
log.write("7890")
with open(self.path) as f:
self.assertEqual(f.read(), "1234567890")
示例12: test_rotation
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
def test_rotation(self):
"""
Rotating log files autorotate after a period of time, and can also be
manually rotated.
"""
# this logfile should rotate every 10 bytes
with contextlib.closing(
logfile.LogFile(self.name, self.dir, rotateLength=10)) as log:
# test automatic rotation
log.write("123")
log.write("4567890")
log.write("1" * 11)
self.assertTrue(os.path.exists("{0}.1".format(self.path)))
self.assertFalse(os.path.exists("{0}.2".format(self.path)))
log.write('')
self.assertTrue(os.path.exists("{0}.1".format(self.path)))
self.assertTrue(os.path.exists("{0}.2".format(self.path)))
self.assertFalse(os.path.exists("{0}.3".format(self.path)))
log.write("3")
self.assertFalse(os.path.exists("{0}.3".format(self.path)))
# test manual rotation
log.rotate()
self.assertTrue(os.path.exists("{0}.3".format(self.path)))
self.assertFalse(os.path.exists("{0}.4".format(self.path)))
self.assertEqual(log.listLogs(), [1, 2, 3])
示例13: test_noPermission
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
def test_noPermission(self):
"""
Check it keeps working when permission on dir changes.
"""
log = logfile.LogFile(self.name, self.dir)
self.addCleanup(log.close)
log.write("abc")
# change permissions so rotation would fail
os.chmod(self.dir, 0o555)
# 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")
示例14: test_fromFullPath
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
def test_fromFullPath(self):
"""
Test the fromFullPath method.
"""
log1 = logfile.LogFile(self.name, self.dir, 10, defaultMode=0o777)
self.addCleanup(log1.close)
log2 = logfile.LogFile.fromFullPath(self.path, 10, defaultMode=0o777)
self.addCleanup(log2.close)
self.assertEqual(log1.name, log2.name)
self.assertEqual(os.path.abspath(log1.path), log2.path)
self.assertEqual(log1.rotateLength, log2.rotateLength)
self.assertEqual(log1.defaultMode, log2.defaultMode)
示例15: test_defaultPermissions
# 需要導入模塊: from twisted.python import logfile [as 別名]
# 或者: from twisted.python.logfile import LogFile [as 別名]
def test_defaultPermissions(self):
"""
Test the default permission of the log file: if the file exist, it
should keep the permission.
"""
with open(self.path, "wb"):
os.chmod(self.path, 0o707)
currentMode = stat.S_IMODE(os.stat(self.path)[stat.ST_MODE])
log1 = logfile.LogFile(self.name, self.dir)
self.assertEqual(stat.S_IMODE(os.stat(self.path)[stat.ST_MODE]),
currentMode)
self.addCleanup(log1.close)