forked from VFPX/Log4VFP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log4vfp.prg
477 lines (432 loc) · 15.1 KB
/
log4vfp.prg
1
SET PROCEDURE TO Log4Vfp ADDITIVERETURN*************************************************************DEFINE CLASS Log4Vfp AS CUSTOM ************************************************************* *: Author: Rick Strahl *: (c) West Wind Technologies, 2018 *:Contact: http://www.west-wind.com *:Created: 09/29/18 ************************************************************* #IF .F. *:Help Documentation *:Topic: CLASS Log4Vfp *:Description: *:Example: *:Remarks: *:SeeAlso: *:ENDHELP #ENDIF oBridge = NULL oLogger = NULL oLogManager = NULL cConfigurationFile = LOWER(FULLPATH("Log4Net.config")) cUser = SUBSTR(SYS(0), AT("#", SYS(0)) + 2) ************************************************************************ * Init **************************************** *** Function: *** Assume: *** Pass: *** Return: ************************************************************************ FUNCTION INIT() *** wwDotnetBridge dependency DO wwDotnetBridge THIS.oBridge = GetwwDotnetBridge() IF VARTYPE(THIS.oBridge) != "O" ERROR "Failed to load wwDotnetBridge." ENDIF *** Enable lThrowOnError THIS.oBridge.lThrowOnError = .T. IF(!THIS.oBridge.LoadAssembly("Log4VFP.dll")) ERROR "Failed to load Log4VFP assembly." ENDIF IF(!THIS.oBridge.LoadAssembly("Npgsql.dll")) ERROR "Failed to load Npgsql assembly." ENDIF THIS.cUser = SUBSTR(SYS(0), AT("#", SYS(0)) + 2) ENDFUNC * Init ************************************************************************ * Open **************************************** *** Function: *** Assume: *** Pass: *** Return: ************************************************************************ FUNCTION OPEN(lcLogFile, lcName) LOCAL loLogManager, loLogger IF(!FILE(THIS.cConfigurationFile)) THIS.CreateConfigurationFile() ENDIF IF EMPTY(lcLogFile) lcLogFile = "" ENDIF IF EMPTY(lcName) lcName = SYS(2015) ENDIF THIS.oLogManager = THIS.oBridge.CreateInstance("Log4VFP.LogManager", ; THIS.cConfigurationFile, ; lcLogFile, ; THIS.cUser) IF VARTYPE(THIS.oLogManager) != "O" ERROR "Unable to create Log Manager: " + THIS.oBridge.cErrorMsg ENDIF THIS.oLogger = THIS.oLogManager.GetLogger(lcName) IF VARTYPE(THIS.oLogger) != "O" ERROR "Unable to create logger: " + THIS.oBridge.cErrorMsg ENDIF RETURN THIS.oLogger ENDFUNC * Open ************************************************************************ * SetProperty **************************************** *** Function: Sets the value of a named property to the specified value (if the property doesn't exists, it's created). *** Assume: *** Pass: tcName - the name of the property, tuValue - the value of the property *** Return: ************************************************************************ FUNCTION SetProperty(tcName, tuValue) THIS.oLogManager.SetProperty(tcName, TRANSFORM(tuValue)) ENDFUNC ************************************************************************ * CreateLogConfiguration **************************************** *** Function: Creates a new configuration file as a template *** Assume: *** Pass: lcFilename - *** Return: ************************************************************************ FUNCTION CreateConfigurationFile(lcFileName) LOCAL lcConfig IF(EMPTY(lcFileName)) lcFileName = THIS.cConfigurationFile ENDIF TEXT TO lcConfig NOSHOW<configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="%property{LogFileName}" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="5" /> <maximumFileSize value="10MB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date (%property{AppStart} seconds since app started, %property{Milestone} seconds since last milestone) %property{CurrentUser} %level - %message%newline" /> </layout> </appender> <root> <level value="INFO"/> <appender-ref ref="RollingFileAppender"/> </root> </log4net></configuration> ENDTEXT TRY STRTOFILE(STRCONV(lcConfig, 9), lcFileName, 4) CATCH ENDTRY RETURN FILE(lcFileName) ENDFUNC * CreateLogConfiguration ************************************************************************ * StartMileStone **************************************** *** Function: *** Assume: *** Pass: *** Return: ************************************************************************ FUNCTION StartMileStone(tcMessage) IF VARTYPE(THIS.oLogManager) = "O" THIS.oLogManager.StartMileStone() IF VARTYPE(tcMessage) = "C" AND ! EMPTY(tcMessage) THIS.LogInfo(tcMessage) ENDIF RETURN .T. ENDIF RETURN .F. ENDFUNC * StartMileStone ************************************************************************ * Shutdown **************************************** *** Function: Shuts down the log Session *** Assume: *** Pass: *** Return: ************************************************************************ FUNCTION SHUTDOWN() IF VARTYPE(THIS.oLogManager) = "O" THIS.oLogManager.SHUTDOWN() THIS.oLogManager = NULL ENDIF THIS.oLogger = .NULL. ENDFUNC * Shutdown ************************************************************************ * Destroy **************************************** *** Function: Shuts down the log Session *** Assume: *** Pass: *** Return: ************************************************************************ FUNCTION DESTROY THIS.SHUTDOWN() ENDFUNC ************************************************************************ * LogInfo **************************************** *** Function: Logs an INFO message *** Assume: *** Pass: The message to log and up to 10 parameters to be inserted *** into placeholders in the message *** Return: ************************************************************************ FUNCTION LogInfo(tcMessage, tuParam1, tuParam2, tuParam3, tuParam4, ; tuParam5, tuParam6, tuParam7, tuParam8, tuParam9, tuParam10) LOCAL lnParams lnParams = PCOUNT() DO CASE CASE VARTYPE(THIS.oLogger) != "O" RETURN .F. CASE lnParams = 1 THIS.oLogger.INFO(tcMessage) CASE lnParams = 2 THIS.oLogger.InfoFormat(tcMessage, tuParam1) CASE lnParams = 3 THIS.oLogger.InfoFormat(tcMessage, tuParam1, tuParam2) CASE lnParams = 4 THIS.oLogger.InfoFormat(tcMessage, tuParam1, tuParam2, tuParam3) CASE lnParams = 5 THIS.oLogger.InfoFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4) CASE lnParams = 6 THIS.oLogger.InfoFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5) CASE lnParams = 7 THIS.oLogger.InfoFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6) CASE lnParams = 8 THIS.oLogger.InfoFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7) CASE lnParams = 9 THIS.oLogger.InfoFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8) CASE lnParams = 10 THIS.oLogger.InfoFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8, tuParam9) CASE lnParams = 11 THIS.oLogger.InfoFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8, tuParam9, ; tuParam10) ENDCASE RETURN .T. ENDFUNC ************************************************************************ * LogError **************************************** *** Function: Logs an ERROR message *** Assume: *** Pass: The message to log and up to 10 parameters to be inserted *** into placeholders in the message *** Return: ************************************************************************ FUNCTION LOGERROR(tcMessage, tuParam1, tuParam2, tuParam3, tuParam4, ; tuParam5, tuParam6, tuParam7, tuParam8, tuParam9, tuParam10) LOCAL lnParams lnParams = PCOUNT() DO CASE CASE VARTYPE(THIS.oLogger) != "O" RETURN .F. CASE lnParams = 1 THIS.oLogger.ERROR(tcMessage) CASE lnParams = 2 THIS.oLogger.ErrorFormat(tcMessage, tuParam1) CASE lnParams = 3 THIS.oLogger.ErrorFormat(tcMessage, tuParam1, tuParam2) CASE lnParams = 4 THIS.oLogger.ErrorFormat(tcMessage, tuParam1, tuParam2, tuParam3) CASE lnParams = 5 THIS.oLogger.ErrorFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4) CASE lnParams = 6 THIS.oLogger.ErrorFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5) CASE lnParams = 7 THIS.oLogger.ErrorFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6) CASE lnParams = 8 THIS.oLogger.ErrorFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7) CASE lnParams = 9 THIS.oLogger.ErrorFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8) CASE lnParams = 10 THIS.oLogger.ErrorFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8, tuParam9) CASE lnParams = 11 THIS.oLogger.ErrorFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8, tuParam9, ; tuParam10) ENDCASE RETURN .T. ENDFUNC ************************************************************************ * LogWarn **************************************** *** Function: Logs a WARN message *** Assume: *** Pass: The message to log and up to 10 parameters to be inserted *** into placeholders in the message *** Return: ************************************************************************ FUNCTION LogWarn(tcMessage, tuParam1, tuParam2, tuParam3, tuParam4, ; tuParam5, tuParam6, tuParam7, tuParam8, tuParam9, tuParam10) LOCAL lnParams lnParams = PCOUNT() DO CASE CASE VARTYPE(THIS.oLogger) != "O" RETURN .F. CASE lnParams = 1 THIS.oLogger.Warn(tcMessage) CASE lnParams = 2 THIS.oLogger.WarnFormat(tcMessage, tuParam1) CASE lnParams = 3 THIS.oLogger.WarnFormat(tcMessage, tuParam1, tuParam2) CASE lnParams = 4 THIS.oLogger.WarnFormat(tcMessage, tuParam1, tuParam2, tuParam3) CASE lnParams = 5 THIS.oLogger.WarnFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4) CASE lnParams = 6 THIS.oLogger.WarnFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5) CASE lnParams = 7 THIS.oLogger.WarnFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6) CASE lnParams = 8 THIS.oLogger.WarnFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7) CASE lnParams = 9 THIS.oLogger.WarnFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8) CASE lnParams = 10 THIS.oLogger.WarnFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8, tuParam9) CASE lnParams = 11 THIS.oLogger.WarnFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8, tuParam9, ; tuParam10) ENDCASE RETURN .T. ENDFUNC ************************************************************************ * LogDebug **************************************** *** Function: Logs a DEBUG message *** Assume: *** Pass: The message to log and up to 10 parameters to be inserted *** into placeholders in the message *** Return: ************************************************************************ FUNCTION LogDebug(tcMessage, tuParam1, tuParam2, tuParam3, tuParam4, ; tuParam5, tuParam6, tuParam7, tuParam8, tuParam9, tuParam10) LOCAL lnParams lnParams = PCOUNT() DO CASE CASE VARTYPE(THIS.oLogger) != "O" RETURN .F. CASE lnParams = 1 THIS.oLogger.DEBUG(tcMessage) CASE lnParams = 2 THIS.oLogger.DebugFormat(tcMessage, tuParam1) CASE lnParams = 3 THIS.oLogger.DebugFormat(tcMessage, tuParam1, tuParam2) CASE lnParams = 4 THIS.oLogger.DebugFormat(tcMessage, tuParam1, tuParam2, tuParam3) CASE lnParams = 5 THIS.oLogger.DebugFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4) CASE lnParams = 6 THIS.oLogger.DebugFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5) CASE lnParams = 7 THIS.oLogger.DebugFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6) CASE lnParams = 8 THIS.oLogger.DebugFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7) CASE lnParams = 9 THIS.oLogger.DebugFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8) CASE lnParams = 10 THIS.oLogger.DebugFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8, tuParam9) CASE lnParams = 11 THIS.oLogger.DebugFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8, tuParam9, ; tuParam10) ENDCASE RETURN .T. ENDFUNC ************************************************************************ * LogFatal **************************************** *** Function: Logs a FATAL message *** Assume: *** Pass: The message to log and up to 10 parameters to be inserted *** into placeholders in the message *** Return: ************************************************************************ FUNCTION LogFatal(tcMessage, tuParam1, tuParam2, tuParam3, tuParam4, ; tuParam5, tuParam6, tuParam7, tuParam8, tuParam9, tuParam10) LOCAL lnParams lnParams = PCOUNT() DO CASE CASE VARTYPE(THIS.oLogger) != "O" RETURN .F. CASE lnParams = 1 THIS.oLogger.Fatal(tcMessage) CASE lnParams = 2 THIS.oLogger.FatalFormat(tcMessage, tuParam1) CASE lnParams = 3 THIS.oLogger.FatalFormat(tcMessage, tuParam1, tuParam2) CASE lnParams = 4 THIS.oLogger.FatalFormat(tcMessage, tuParam1, tuParam2, tuParam3) CASE lnParams = 5 THIS.oLogger.FatalFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4) CASE lnParams = 6 THIS.oLogger.FatalFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5) CASE lnParams = 7 THIS.oLogger.FatalFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6) CASE lnParams = 8 THIS.oLogger.FatalFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7) CASE lnParams = 9 THIS.oLogger.FatalFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8) CASE lnParams = 10 THIS.oLogger.FatalFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8, tuParam9) CASE lnParams = 11 THIS.oLogger.FatalFormat(tcMessage, tuParam1, tuParam2, tuParam3, ; tuParam4, tuParam5, tuParam6, tuParam7, tuParam8, tuParam9, ; tuParam10) ENDCASE RETURN .T. ENDFUNCENDDEFINE*EOC Log4Vfp