CFLogAdapter

Package: MachII.logging.adapters
Inherits from: logging.adapters.AbstractLogAdapter
A concrete adapter for CFLog.

<!--- License: Copyright 2008 GreatBizTools, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Copyright: GreatBizTools, LLC Author: Peter J. Farrell (peter@mach-ii.com) $Id: CFLogAdapter.cfc 932 2008-07-31 06:41:20Z peterfarrell $ Created version: 1.6.0 Updated version: 1.6.0 Notes: Special thanks to the Simple Log in Apache Commons Logging project for inspiration for this component. --->

Method Summary
public void configure()

Configures the adapter.

public void debug(string channel, string message, [any additionalInformation])

Logs a message with debug log level.

public void error(string channel, string message, [any additionalInformation])

Logs a message with error log level.

public void fatal(string channel, string message, [any additionalInformation])

Logs a message with fatal log level.

public boolean getDebugModeOnly()

Gets if the adapter will log if the CFML server debug mode is enabled.

private numeric getLevel()

Returns the internal numeric log level.

public string getLogFile()

Gets the value for the cflog 'file' attribute

public string getLoggingLevel()

Gets the logging level by name.

public void info(string channel, string message, [any additionalInformation])

Logs a message with info log level.

public boolean isDebugEnabled()

Checks if debug level logging is enabled.

public boolean isErrorEnabled()

Checks if error level logging is enabled.

public boolean isFatalEnabled()

Checks if fatal level logging is enabled.

public boolean isInfoEnabled()

Checks if info level logging is enabled.

private boolean isLevelEnabled(numeric logLevel)

Checks if the passed log level is enabled.

public boolean isTraceEnabled()

Checks if trace level logging is enabled.

public boolean isWarnEnabled()

Checks if warn level logging is enabled.

private void logMessage(string channel, numeric logLevel, string message, [any additionalInformation])

Logs a message.

private void setDebugModeOnly(boolean debugModeOnly)

Sets if the adapter will log if the CFML server debug mode is enabled.

private void setLevel(numeric level)

Sets the internal numeric log level.

private void setLogFile(string logFile)

Sets the value for the cflog 'file' attribute.

public void setLoggingLevel(string loggingLevelName)

Sets the logging level by name.

public void trace(string channel, string message, [any additionalInformation])

Logs a message with trace log level.

private string translateLevelToName(numeric level)

Translate a numerical logging level to human readable string.

private string translateLogLevelToCFLogType(numeric logLevel)

Translates a log level to a human readable string.

public void warn(string channel, string message, [any additionalInformation])

Logs a message with warn log level.

Methods inherited from logging.adapters.AbstractLogAdapter:   isParameterDefined , setParameter , setLoggingEnabled , init , setParameters , getFilter , getConfigurationData , getParameter , isFilterDefined , setFilter , getParameters , getLoggingEnabled
Method Detail
configure

public void configure( )

Configures the adapter.

Parameters:

Code:

	<cffunction name="configure" access="public" returntype="void" output="false"
		hint="Configures the adapter.">
		
		<cfif isParameterDefined("logFile")>
			<cfset setlogFile(getParameter("logFile")) />
		</cfif>
		<cfif isParameterDefined("loggingLevel")>
			<cfset setLoggingLevel(getParameter("loggingLevel")) />
		</cfif>
		<cfif isParameterDefined("loggingEnabled")>
			<cfset setLoggingEnabled(getParameter("loggingEnabled")) />
		</cfif>
		<cfif isParameterDefined("debugModeOnly")>
			<cfif NOT IsBoolean(getParameter("debugModeOnly"))>
				<cfthrow type="MachII.logging.strategies.MachIILog.Logger"
					message="The value of 'debugModeOnly' must be boolean."
					detail="Current value '#getParameter('debugModeOnly')#'" />
			<cfelse>
				<cfset setDebugModeOnly(getParameter("debugModeOnly")) />
			</cfif>
		</cfif>
	</cffunction> 

debug

public void debug( string channel, string message, [any additionalInformation] )

Logs a message with debug log level.

Parameters:
string channel
string message
[any additionalInformation]

Code:

	<cffunction name="debug" access="public" returntype="void" output="false"
		hint="Logs a message with debug log level.">
		<cfargument name="channel" type="string" required="true" />
		<cfargument name="message" type="string" required="true" />
		<cfargument name="additionalInformation" type="any" required="false" />
		
		<cfif isDebugEnabled()>
			<cfif StructKeyExists(arguments, "additionalInformation")>
				<cfset logMessage(arguments.channel, variables.LOG_LEVEL_DEBUG, arguments.message, arguments.additionalInformation) />
			<cfelse>
				<cfset logMessage(arguments.channel, variables.LOG_LEVEL_DEBUG, arguments.message) />
			</cfif>
		</cfif>
	</cffunction> 

error

public void error( string channel, string message, [any additionalInformation] )

Logs a message with error log level.

Parameters:
string channel
string message
[any additionalInformation]

Code:

	<cffunction name="error" access="public" returntype="void" output="false"
		hint="Logs a message with error log level.">
		<cfargument name="channel" type="string" required="true" />
		<cfargument name="message" type="string" required="true" />
		<cfargument name="additionalInformation" type="any" required="false" />
		
		<cfif isErrorEnabled()>
			<cfif StructKeyExists(arguments, "additionalInformation")>
				<cfset logMessage(arguments.channel, variables.LOG_LEVEL_ERROR, arguments.message, arguments.additionalInformation) />
			<cfelse>
				<cfset logMessage(arguments.channel, variables.LOG_LEVEL_ERROR, arguments.message) />
			</cfif>
		</cfif>
	</cffunction> 

fatal

public void fatal( string channel, string message, [any additionalInformation] )

Logs a message with fatal log level.

Parameters:
string channel
string message
[any additionalInformation]

Code:

	<cffunction name="fatal" access="public" returntype="void" output="false"
		hint="Logs a message with fatal log level.">
		<cfargument name="channel" type="string" required="true" />
		<cfargument name="message" type="string" required="true" />
		<cfargument name="additionalInformation" type="any" required="false" />
		
		<cfif isFatalEnabled()>
			<cfif StructKeyExists(arguments, "additionalInformation")>
				<cfset logMessage(arguments.channel, variables.LOG_LEVEL_FATAL, arguments.message, arguments.additionalInformation) />
			<cfelse>
				<cfset logMessage(arguments.channel, variables.LOG_LEVEL_FATAL, arguments.message) />
			</cfif>
		</cfif>
	</cffunction> 

getDebugModeOnly

public boolean getDebugModeOnly( )

Gets if the adapter will log if the CFML server debug mode is enabled.

Parameters:

Code:

	<cffunction name="getDebugModeOnly" access="public" returntype="boolean" output="false"
		hint="Gets if the adapter will log if the CFML server debug mode is enabled.">
		<cfreturn variables.debugModeOnly />
	</cffunction> 

getLevel

private numeric getLevel( )

Returns the internal numeric log level.

Parameters:

Code:

	<cffunction name="getLevel" access="private" returntype="numeric" output="false"
		hint="Returns the internal numeric log level.">
		<cfreturn variables.level />
	</cffunction> 

getLogFile

public string getLogFile( )

Gets the value for the cflog 'file' attribute

Parameters:

Code:

	<cffunction name="getLogFile" access="public" returntype="string" output="false"
		hint="Gets the value for the cflog 'file' attribute">
		<cfreturn variables.instance.logFile />
	</cffunction> 

getLoggingLevel

public string getLoggingLevel( )

Gets the logging level by name.

Parameters:

Code:

	<cffunction name="getLoggingLevel" access="public" returntype="string" output="false"
		hint="Gets the logging level by name.">
		<cfreturn translateLevelToName(getLevel()) />
	</cffunction> 

info

public void info( string channel, string message, [any additionalInformation] )

Logs a message with info log level.

Parameters:
string channel
string message
[any additionalInformation]

Code:

	<cffunction name="info" access="public" returntype="void" output="false"
		hint="Logs a message with info log level.">
		<cfargument name="channel" type="string" required="true" />
		<cfargument name="message" type="string" required="true" />
		<cfargument name="additionalInformation" type="any" required="false" />

		<cfif isInfoEnabled()>
			<cfif StructKeyExists(arguments, "additionalInformation")>
				<cfset logMessage(arguments.channel, variables.LOG_LEVEL_INFO, arguments.message, arguments.additionalInformation) />
			<cfelse>
				<cfset logMessage(arguments.channel, variables.LOG_LEVEL_INFO, arguments.message) />
			</cfif>
		</cfif>
	</cffunction> 

isDebugEnabled

public boolean isDebugEnabled( )

Checks if debug level logging is enabled.

Parameters:

Code:

	<cffunction name="isDebugEnabled" access="public" returntype="boolean" output="false"
		hint="Checks if debug level logging is enabled.">
		<cfreturn isLevelEnabled(variables.LOG_LEVEL_DEBUG) />
	</cffunction> 

isErrorEnabled

public boolean isErrorEnabled( )

Checks if error level logging is enabled.

Parameters:

Code:

	<cffunction name="isErrorEnabled" access="public" returntype="boolean" output="false"
		hint="Checks if error level logging is enabled.">
		<cfreturn isLevelEnabled(variables.LOG_LEVEL_ERROR) />
	</cffunction> 

isFatalEnabled

public boolean isFatalEnabled( )

Checks if fatal level logging is enabled.

Parameters:

Code:

	<cffunction name="isFatalEnabled" access="public" returntype="boolean" output="false"
		hint="Checks if fatal level logging is enabled.">
		<cfreturn isLevelEnabled(variables.LOG_LEVEL_FATAL) />
	</cffunction> 

isInfoEnabled

public boolean isInfoEnabled( )

Checks if info level logging is enabled.

Parameters:

Code:

	<cffunction name="isInfoEnabled" access="public" returntype="boolean" output="false"
		hint="Checks if info level logging is enabled.">
		<cfreturn isLevelEnabled(variables.LOG_LEVEL_INFO) />
	</cffunction> 

isLevelEnabled

private boolean isLevelEnabled( numeric logLevel )

Checks if the passed log level is enabled.

Parameters:
numeric logLevel

Code:

	<cffunction name="isLevelEnabled" access="private" returntype="boolean" output="false"
		hint="Checks if the passed log level is enabled.">
		<cfargument name="logLevel" type="numeric" required="true"
			hint="Log levels are numerically ordered for easier comparison." />
		<cfif getLoggingEnabled() AND ((getDebugModeOnly() AND IsDebugMode()) OR NOT getDebugModeOnly())>
			<cfreturn arguments.logLevel GTE getLevel() />
		<cfelse>
			<cfreturn false />
		</cfif>
	</cffunction> 

isTraceEnabled

public boolean isTraceEnabled( )

Checks if trace level logging is enabled.

Parameters:

Code:

	<cffunction name="isTraceEnabled" access="public" returntype="boolean" output="false"
		hint="Checks if trace level logging is enabled.">
		<cfreturn isLevelEnabled(variables.LOG_LEVEL_TRACE) />
	</cffunction> 

isWarnEnabled

public boolean isWarnEnabled( )

Checks if warn level logging is enabled.

Parameters:

Code:

	<cffunction name="isWarnEnabled" access="public" returntype="boolean" output="false"
		hint="Checks if warn level logging is enabled.">
		<cfreturn isLevelEnabled(variables.LOG_LEVEL_WARN) />
	</cffunction> 

logMessage

private void logMessage( string channel, numeric logLevel, string message, [any additionalInformation] )

Logs a message.

Parameters:
string channel
numeric logLevel
string message
[any additionalInformation]

Code:

	<cffunction name="logMessage" access="private" returntype="void" output="false"
		hint="Logs a message.">
		<cfargument name="channel" type="string" required="true" />
		<cfargument name="logLevel" type="numeric" required="true" />
		<cfargument name="message" type="string" required="true" />
		<cfargument name="additionalInformation" type="any" required="false" />
		
		<cfset var type = translateLogLevelToCFLogType(arguments.logLevel) />
		<cfset var text = "[" & arguments.channel & "] " />
		
		
		<cfif NOT isFilterDefined() OR getFilter().decide(arguments)>
			
			<cfif arguments.logLevel EQ 1>
				<cfset text = text & "(Trace) " />
			<cfelseif arguments.logLevel EQ 2>
				<cfset text = text & "(Debug) " />
			<cfelseif arguments.logLevel EQ 3>
				<cfset text = text & "(Info) " />
			</cfif>
			
			
			<cfset text = text & arguments.message />
			
			
			<cfif StructKeyExists(arguments, "additionalInformation")>
				<cftry>
					<cfset text = text & " :: " & arguments.additionalInformation.toString() />
					<cfcatch type="any">
						
							<cfset text = text & " :: [Complex Value]" />
					</cfcatch>
				</cftry>
			</cfif>
			
			
			<cflog type="#type#" text="#text#" file="#getLogFile()#" />
		</cfif>
	</cffunction> 

setDebugModeOnly

private void setDebugModeOnly( boolean debugModeOnly )

Sets if the adapter will log if the CFML server debug mode is enabled.

Parameters:
boolean debugModeOnly

Code:

	<cffunction name="setDebugModeOnly" access="private" returntype="void" output="false"
		hint="Sets if the adapter will log if the CFML server debug mode is enabled.">
		<cfargument name="debugModeOnly" type="boolean" required="true" />
		<cfset variables.debugModeOnly = arguments.debugModeOnly />
	</cffunction> 

setLevel

private void setLevel( numeric level )

Sets the internal numeric log level.

Parameters:
numeric level

Code:

	<cffunction name="setLevel" access="private" returntype="void" output="false"
		hint="Sets the internal numeric log level.">
		<cfargument name="level" type="numeric" required="true"
			hint="Accepts an integer 0 through 7" />
		
		<cfif NOT REFind("^([0-7]{1})$", arguments.level)>
			<cfthrow message="The argument named 'level' accepts an integer 0 through 7."
				detail="Passed value:#arguments.level#" />
		</cfif>
		
		<cfset variables.level = arguments.level />
	</cffunction> 

setLogFile

private void setLogFile( string logFile )

Sets the value for the cflog 'file' attribute.

Parameters:
string logFile

Code:

	<cffunction name="setLogFile" access="private" returntype="void" output="false"
		hint="Sets the value for the cflog 'file' attribute.">
		<cfargument name="logFile" type="string" required="true" />
		<cfset variables.instance.logFile = arguments.logFile />
	</cffunction> 

setLoggingLevel

public void setLoggingLevel( string loggingLevelName )

Sets the logging level by name.

Parameters:
string loggingLevelName

Code:

	<cffunction name="setLoggingLevel" access="public" returntype="void" output="false"
		hint="Sets the logging level by name.">
		<cfargument name="loggingLevelName" type="string" required="true"
			hint="Accepts 'trace', 'debug', 'info', 'warn', 'error', 'fatal', 'all' or 'off'." />
		
		<cfset var level = "" />
		
		<cfif NOT ListFindNoCase("trace|debug|info|warn|error|fatal|all|off",  arguments.loggingLevelName, "|")>
			<cfthrow message="The argument named 'loggingLevelName' accepts 'trace', 'debug', 'info', 'warn', 'error', 'fatal', 'all' or 'off'."
				detail="Passed value:#arguments.loggingLevelName#" />
		</cfif>
		
		<cfif arguments.loggingLevelName EQ "trace">
			<cfset level = 1 />
		<cfelseif  arguments.loggingLevelName EQ "debug">
			<cfset level = 2 />
		<cfelseif  arguments.loggingLevelName EQ "info">
			<cfset level = 3 />
		<cfelseif  arguments.loggingLevelName EQ "warn">
			<cfset level = 4 />
		<cfelseif  arguments.loggingLevelName EQ "error">
			<cfset level = 5 />
		<cfelseif  arguments.loggingLevelName EQ "fatal">
			<cfset level = 6 />
		<cfelseif  arguments.loggingLevelName EQ "all">
			<cfset level = 0 />
		<cfelseif  arguments.loggingLevelName EQ "off">
			<cfset level = 7 />
		</cfif>
		
		
		<cfset setLevel(level) />
	</cffunction> 

trace

public void trace( string channel, string message, [any additionalInformation] )

Logs a message with trace log level.

Parameters:
string channel
string message
[any additionalInformation]

Code:

	<cffunction name="trace" access="public" returntype="void" output="false"
		hint="Logs a message with trace log level.">
		<cfargument name="channel" type="string" required="true" />
		<cfargument name="message" type="string" required="true" />
		<cfargument name="additionalInformation" type="any" required="false" />

		<cfif isTraceEnabled()>
			<cfif StructKeyExists(arguments, "additionalInformation")>
				<cfset logMessage(arguments.channel, variables.LOG_LEVEL_TRACE, arguments.message, arguments.additionalInformation) />
			<cfelse>
				<cfset logMessage(arguments.channel, variables.LOG_LEVEL_TRACE, arguments.message) />
			</cfif>
		</cfif>
	</cffunction> 

translateLevelToName

private string translateLevelToName( numeric level )

Translate a numerical logging level to human readable string.

Parameters:
numeric level

Code:

	<cffunction name="translateLevelToName" access="private" returntype="string" output="false"
		hint="Translate a numerical logging level to human readable string.">
		<cfargument name="level" type="numeric" required="true" />

		<cfset var loggingLevelName = "" />
		
		<cfif arguments.level EQ 1>
			<cfset loggingLevelName = "trace" />
		<cfelseif  arguments.level EQ 2>
			<cfset loggingLevelName = "debug" />
		<cfelseif  arguments.level EQ 3>
			<cfset loggingLevelName = "info" />
		<cfelseif  arguments.level EQ 4>
			<cfset loggingLevelName = "warn" />
		<cfelseif  arguments.level EQ 5>
			<cfset loggingLevelName = "error" />
		<cfelseif  arguments.level EQ 6>
			<cfset loggingLevelName = "fatal" />
		<cfelseif  arguments.level EQ 0>
			<cfset loggingLevelName = "all" />
		<cfelseif  arguments.level EQ 7>
			<cfset loggingLevelName = "off" />
		</cfif>
		
		<cfreturn loggingLevelName />
	</cffunction> 

translateLogLevelToCFLogType

private string translateLogLevelToCFLogType( numeric logLevel )

Translates a log level to a human readable string.

Parameters:
numeric logLevel

Code:

	<cffunction name="translateLogLevelToCFLogType" access="private" returntype="string" output="false"
		hint="Translates a log level to a human readable string.">
		<cfargument name="logLevel" type="numeric" required="true" />
		
		<cfset var result = "" />
		
		<cfif arguments.logLevel EQ 1>
			<cfset result = "Information" />
		<cfelseif arguments.logLevel EQ 2>
			<cfset result = "Information" />
		<cfelseif arguments.logLevel EQ 3>
			<cfset result = "Information" />
		<cfelseif arguments.logLevel EQ 4>
			<cfset result = "Warning" />
		<cfelseif arguments.logLevel EQ 5>
			<cfset result = "Error" />
		<cfelseif arguments.logLevel EQ 6>
			<cfset result = "Fatal" />
		</cfif>
		
		<cfreturn result />
	</cffunction> 

warn

public void warn( string channel, string message, [any additionalInformation] )

Logs a message with warn log level.

Parameters:
string channel
string message
[any additionalInformation]

Code:

	<cffunction name="warn" access="public" returntype="void" output="false"
		hint="Logs a message with warn log level.">
		<cfargument name="channel" type="string" required="true" />
		<cfargument name="message" type="string" required="true" />
		<cfargument name="additionalInformation" type="any" required="false" />

		<cfif isWarnEnabled()>
			<cfif StructKeyExists(arguments, "additionalInformation")>
				<cfset logMessage(arguments.channel, variables.LOG_LEVEL_WARN, arguments.message, arguments.additionalInformation) />
			<cfelse>
				<cfset logMessage(arguments.channel, variables.LOG_LEVEL_WARN, arguments.message) />
			</cfif>
		</cfif>
	</cffunction>