vbScript - Create a Log File Using vbScript Classes
Before I retired I was responsible for most of the corporate data plumbing at our control centre. All of the plumbing was written in vbScript. As any maintenance programmer knows, log files are a vital key in identifying and fixing problems. Because I am basically lazy I don't like to code the same process over and over so I created logging code that could be included in any script. The scripts were run automatically on periods ranging from once every five minutes to once a week, or even as little as once a month. Depending on the activity, log files had the potential of becoming very large and unmanageable so I added the option of creating separate log files based on the day, month, or even year. To keep the names standardized I made the default log file name the same as the script that was creating it, except with a .log
file extension as well as a custom tag based on the log type. Logs can be of type daily, monthly, yearly, or forever and the file names are modified as follows:
daily <scriptname> YYYY-MM-DD.log
monthly <scriptname> YYYY-MM.log
yearly <scriptname> YYYY.log
forever <scriptname>.log
Just as in the full vb.Net, in vbScript, you can define classes and instantiate them at run time. You won't have the full set of features you would find in vb.Net, but enough to be useful. A class in vbScript is defined as follows
Class classname
.
.
.
End Class
Inside you define properties and methods, again, similar to vb.Net but with restrictions. For example, in vbScript there is no function overloading. Properties and methods can be declared Public (available outside the class) or Private (available only within the class). Properties can be read/write or read only. To declare a public property you do
Public varname
and you refer to that property as obj.varname
as you would expect. Purists will say that you should never declare a public property. You should declare a private property then access it using Let/Set/Get interfaces. For example
Class myClass
Private m_Name
Public Property Let Name(val)
m_Name = val
End Property
Public Property Get Name()
Name = m_Name
End Property
End Class
This give you a public property, Name
that is read/write. To make it read only just remove the Let
block. Set
is the same as Let
except that it is used for object properties. Lastly I want to discuss constructors and destructors. Any code you want to run when the object is created must go inside the sub
Private Sub Class_Initialize ()
.
.
.
End Sub
and any cleanup that you want to do when the object is destroyed must go inside
Private Sub Class_Terminate()
.
.
.
End Sub
Methods can also be public or private. You'll notice that my private property is prefixed with m_
. This is simply a convention I follow to visually distinguish public properties and methods from private ones.
So much for the quickie tutorial. Let me just touch briefly on how to use the logger class.
You create an instance of the logger object by
Set logger = New clsLogger
and then, unless you want the default log type, you set the type either by
logger.LogType = "daily"
or
logger.Open("daily")
If you are expecting low logger activity you can use the former, and the log file will be opened and closed for each write. If you are expecting higher activity you can use the latter and manually close it when done, or let the destructor close the file. You write events to the log file by one of
logger.Write(text)
logger.Tee(dest,text)
The first writes text
to the log file. The second writes it to either the log file (dest="F"), the console (dest="C") or both (dest="B"). The string text
can contain vbCrLr characters, or more conveniently, \n
which will be translated to vbCrLf. Every line is written, prefixed with the current date and time as YYYY-MM-DD HH:MM:SS
. You can also write a separator line by
logger.Separator()
which writes a line of the form
2018-07-25 10:38:00 -------------------- D:\utils\vb.wsf RUNNING ON JIM-PC