Option Explicit
Public Enum LogLevel
logDebug
logInfo
logwarn
logError
logFatal
logNone
End Enum
Private mLogLevel As LogLevel
Const gLogToImmediate = False
Private mLogFullPath As String
Public Sub SetLogLevel(ByVal sLogLevel As String)
Const sRoutineName As String = "SetLogLevel"
Dim enumLogLevel As LogLevel
On Error GoTo ErrHandler
Select Case sLogLevel
Case "logDebug"
enumLogLevel = logDebug
Case "logInfo"
enumLogLevel = logInfo
Case "logWarn"
enumLogLevel = logwarn
Case "logError"
enumLogLevel = logError
Case "logFatal"
enumLogLevel = logFatal
Case "logNone"
enumLogLevel = logNone
Case Else
Err.Raise Errors.BadLogLevelChosen, sRoutineName, sLogLevel & " is not a valid log level"
End Select
mLogLevel = enumLogLevel
Logger.Log "Log level set to " & sLogLevel & " i.e. " & mLogLevel, logInfo, True, True
Exit Sub
ErrHandler:
ErrPersist.ReraiseError sRoutineName & " line:" & Erl & ", "
End Sub
Private Sub SetLogFullPath()
mLogFullPath = GetLogDir() & GetTitle() & ".log"
End Sub
Public Function GetLogDir() As String
GetLogDir = Globals.GetTempDir()
End Function
Public Function GetLogFullPath() As String
GetLogFullPath = GetLogDir() & GetTitle() & ".log"
End Function
Public Sub Log(ByVal logStr As String, ByVal loggingLevel As LogLevel, Optional ByVal displayOnStatusBar As Boolean = False, Optional ByVal displayInImmediate As Boolean = False)
'TODO:LOW update the error handling here. If logging fails it isn't critical for now.
Dim logStrWithTime As String
logStrWithTime = Now & "| " & logStr
On Error Resume Next
If loggingLevel >= mLogLevel Then
'TODO:LOW Look into making this better
If mLogFullPath = "" Then SetLogFullPath
Open mLogFullPath For Append As 1
Print #1, logStrWithTime
End If
If displayOnStatusBar Then
Application.StatusBar = logStrWithTime
End If
If displayInImmediate And gLogToImmediate Then
Debug.Print logStr
End If
On Error GoTo 0
End Sub
Public Sub CloseLogFile()
On Error Resume Next
Close 1
End Sub
Public Sub DeleteLargeLogFile(ByVal lAcceptableFileSzInBytes As Long)
On Error Resume Next
Dim sFile As String
sFile = GetLogFullPath()
Debug.Print FileLen(sFile)
If FileLen(sFile) > lAcceptableFileSzInBytes Then
DeleteFile sFile
End If
End Sub
Private Sub DeleteFile(sDeleteFile As String)
On Error Resume Next
If Len(Dir$(sDeleteFile)) > 0 Then
SetAttr sDeleteFile, vbNormal
Kill sDeleteFile
End If
End Sub
Recent Topics
-
Stock data from yahoo financeEdisonf - Today, 02:57 PM
-
Posting on Craigslist with different proxiesphpnoob - Today, 11:49 AM
-
UML implementation in DBabderrahim - Today, 10:20 AM
-
Use data from a filetzina - Today, 09:47 AM
-
how to make the square not to hit the wallsScareCrawExtended - Today, 09:38 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- Managed C++
- networking
- c
- stream
- programming
- Visual Basic 4 / 5 / 6
- console
- Connection
- import
- authentication
- element
- syntax
- hardware
- session
- sql
- javascript
- array
- printing
- generator
- game
- header
- html5
- mysql
- string
- c++
- timer
- loop
- java
- html
- ajax
- form
- C#
- jquery
- php
- assembly
- linked list
- vb.net
- xml
- android
- css
- login
- encryption
- setup
- calculator
- combobox
- binary
- pseudocode
- hello world
- innerHTML
- grid
#1
Posted 31 January 2013 - 03:58 AM
#2
Posted 31 January 2013 - 08:56 PM
I don't remember if in VBA you can set your enum as string, but if you could you wouldn't need the switch ![]()
and just in case, in the deletelargefile maybe you should check if the file isn't already open, because that will generate an error
#3
Posted 01 February 2013 - 02:25 AM
Thanks for responding. Unfortunately Enums can't have strings in VBA. I've got the on error resume next in the delete log file methods. If the file is locked I won't delete it. I kind of treated logging as something which if it fails its not critical. How would you change it?
#4
Posted 01 February 2013 - 08:09 PM
Maybe you can make log files with the date, like: 2013-02-01.log and so on, and then every time you start you logs beyond a month.
#5
Posted 04 February 2013 - 04:11 AM
Thanks I might include the date as suggested. I'll update this post. Anyone feel free to update this post to make it better.
#6
Posted 06 March 2013 - 02:45 AM
Option Explicit
Public Enum LogLevel logdebug logInfo
logwarn logError logFatal logNoneEnd Enum
Private mLogLevel As LogLevelConst gLogToImmediate = FalsePrivate
mLogFullPath As StringPrivate mFileNum As IntegerPublic Sub
SetLogLevel(ByVal sLogLevel As String)
Const sRoutineName As String = "SetLogLevel" Dim
enumLogLevel As LogLevel On Error GoTo
ErrHandler Select Case
sLogLevel Case
"logDebug" enumLogLevel = logdebug
Case "logInfo" enumLogLevel =
logInfo Case "logWarn" enumLogLevel
= logwarn Case "logError"
enumLogLevel = logError Case
"logFatal" enumLogLevel = logFatal
Case "logNone" enumLogLevel =
logNone Case Else Err.Raise
Errors.BadLogLevelChosen, sRoutineName, sLogLevel & " is not a valid log
level" End Select mLogLevel =
enumLogLevel Logger.Log "Log level set to " & sLogLevel & "
i.e. " & mLogLevel, logInfo, True, TrueExit Sub
ErrHandler: ErrPersist.ReraiseError sRoutineName & " line:"
& Erl & ", "
End Sub
Public Function GetLogLevel(ByVal eLogLevel As LogLevel) As
String Select Case
eLogLevel Case
LogLevel.logdebug GetLogLevel =
"logDebug" Case
LogLevel.logInfo GetLogLevel =
"logInfo" Case
LogLevel.logwarn GetLogLevel =
"logWarn" Case
LogLevel.logError GetLogLevel =
"logError" Case
LogLevel.logFatal GetLogLevel =
"logFatal" Case
LogLevel.logNone GetLogLevel =
"logNone" End Select End Function
Private Sub SetLogFullPath() mLogFullPath = GetLogDir() & "\"
& GetTitle() & ".log"End Sub
Public Function GetLogDir() As StringGetLogDir = Environ("TEMP")End
Function
Public Function GetLogFullPath() As String GetLogFullPath =
GetLogDir() & "\" & GetTitle() & ".log"End Function
Public Sub Log(ByVal logStr As String, ByVal loggingLevel As LogLevel,
Optional ByVal displayOnStatusBar As Boolean = False, Optional ByVal
displayInImmediate As Boolean = False)
'TODO:LOW update the error handling here. If logging fails it isn't critical
for now.Dim logStrWithTime As StringDim logStrWithLvlAndTime As
String
On Error Resume Next
If loggingLevel >= mLogLevel Then 'TODO:LOW Look into making
this better
If mLogFullPath = "" Then SetLogFullPath
logStrWithLvlAndTime = Now & "| " & GetLogLevel(loggingLevel) & " |
" & logStr mFileNum = FreeFile Open mLogFullPath For
Append As #mFileNum Print #mFileNum, logStrWithLvlAndTime
Close #mFileNum DoEventsEnd If
If displayOnStatusBar Then logStrWithTime = Now & "| " &
logStr Application.StatusBar = logStrWithTimeEnd If
If displayInImmediate And gLogToImmediate Then Debug.Print
logStrEnd If
On Error GoTo 0
End Sub
Public Sub CloseLogFile() On Error Resume Next Close
#mFileNumEnd Sub
Public Sub DeleteLargeLogFile(ByVal lAcceptableFileSzInBytes As Long)On
Error Resume Next Dim sFile As String sFile =
GetLogFullPath() Debug.Print FileLen(sFile) If
FileLen(sFile) > lAcceptableFileSzInBytes Then
DeleteFile sFile End IfEnd Sub
Private Sub DeleteFile(sDeleteFile As String) On Error Resume
Next If Len(Dir$(sDeleteFile)) > 0 Then
SetAttr sDeleteFile, vbNormal Kill sDeleteFile
End IfEnd Sub
I updated it so I can tail the log. Closes the file and uses free file num now and also I can see what level of log it is in the log so will be able to filter if need be. It seems better. Not sure if I need doevents or not.
Edited by christhedon, 06 March 2013 - 02:46 AM.
#7
Posted 06 March 2013 - 08:23 PM
I think you got it! pin yourself a medal, you've done a good logger.
I wonder what will you be doing next
#8
Posted 28 March 2013 - 11:31 PM
No, there is no option from within the application to enable logging. You are right, you have to use the Event Viewer.
Whenever there is a application crash, the error message would contain the log file for the same.
The reason why Outlook/Outlook express has a logging feature is because it will trace the path for email sent/recieved and find if the error lies in the application or other network devices.
Similar Topics
-

Excel sheet like GUI object
Started by quotidian, 12 May 2013 In: General Forums → General Programming
gui
- 5 replies
- 206 views
-
Reading Excel Files in C#
Started by NexusNemesis, 10 Sep 2012 In: Tutorial Forums → C# Tutorials
C#
- 2 replies
- 16,107 views
-
How to make data logger using Borland C++ Builder and Excel
Started by muazump, 17 Mar 2013 In: Language Forums → C and C++
c++, data, logger, borland and 2 more...
- 1 reply
- 376 views
-
Autofill PDF Form using Excel data
Started by NadineMicallef, 08 Mar 2013 In: Language Forums → Visual Basic
macro, excel, data, autofill
- 2 replies
- 574 views
-
Cannot delete a style from Excel
Started by christhedon, 06 Feb 2013 In: Language Forums → Visual Basic
excel style vba
- 5 replies
- 261 views















