Welcome, guest | Sign In | My Account | Store | Cart

This class is a basic Singleton log file creator. It allows separate classes/modules to log their activities to the same file (even the same line if they want to).

Python, 81 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import	os, os.path, sys, time

class	Log:

	class	Log_File:
		def	__init__(self, fPath = os.getcwd(), fName = "log.txt"):
			"""	I intend to add some customization features to this,
					e.g., allowing the log file's name and directory to be
					specified, but that will be later
			"""
			self.fpath	=	fPath		#	the class variable, file path
			self.fname	=	fName		#	the class variable, file name
			self.log_file	=	""	#	the class variable, log file
			if	not os.path.isdir(self.fpath):
				self.fpath	=	os.getcwd()
			if	not os.path.isdir(self.fpath):
				sys.stderr.write ("in LogFile initialization\n")
				sys.stderr.write ("  file path set to: <<" + self.fpath + ">>\n")
				sys.stderr.write ("  This appears to not be a valid directory.")
				sys.stderr.write ("  This is a serious internal error")
				fail ("Log file path must be valid")

			if	os.path.isfile(os.path.join(self.fpath, fName)):
				self.fname	=	fName
			else:
				self.fname	=	"log.txt"
			tfName	=	os.path.join(self.fpath, self.fname)
			if	os.path.isfile(tfName):
				size	=	os.path.getsize(tfName)
				if	 size > 65536:
					mode	=	'w+'
				else:
					mode	=	'a+'
			else:
				failIf (os.path.exists(tfName), "file <<" + tfName + ">> exists, "
						+	"but is not a file (possibly a directory?)")
				mode	=	'w+'
			self.log_file	=	open(tfName, mode)
			self.log_file.write(".-=-" * 20 + "\n\n")
			self.log_file.write("log start at: " + time.ctime()
										+ "\n\n")
		
		def	put(self, text = ""):
			if	len(text) > 0:
				self.log_file.write(text)
		
		def	get_id(self):
			return	id(self)
	__log	=	None
	def	__init__(self):
		if	Log.__log == None:
			Log.__log	=	Log.Log_File()
	
	def	get_id(self):
		return	Log.__log.get_id()
	
	def	put(self, text = ""):
		Log.__log.put(text)
	
	def	checkpt(self, text = ""):
		Log.__log.put(time.ctime() + ": " + text + "\n")
	
	def	write(self, text = ""):
		Log.__log.put(text + "\n")
	

if	__name__ == "__main__":
	log_App	=	Log()
	print	"log_App dict::", log_App.__dict__
	print "log_App id = ", log_App.get_id()
	bog_App	=	Log()
	print "log_App id = ", log_App.get_id()
	print "bog_App id = ", bog_App.get_id()
	log_App.write("Once upon a midnight dreary")
	bog_App.write("As I pondered, weak and weary")
	log_App.write("Over many a quaint and curious volumn of forgotten lore")
	bog_App.write("Suddenly there came a tapping")
	log_App.write("As of someone gently rapping")
	log_App.put("\nIn the same line")
	bog_App.put(" I write from both")
	log_App.write(" copies")

This is a quite basic log file creator, intended to assist in program development. It is a singleton class so that if multiple log files are created, they will all write to the same file. (I solved the name conflict possibility by denying the ability to change the file name.) FWIW, this code is intended to be read with tab size set to 3 spaces.

1 comment

Martin Miller 22 years, 2 months ago  # | flag

failIf? The code shown uses a function named "failIf". There is one by that name in the unittest module, is that what was intended?