ScopeAdapter

Package: MachII.logging.adapters
Inherits from: logging.adapters.AbstractLogAdapter
A concrete adapter for scope logging. Logs messages to a scope.

<!--- 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: ScopeAdapter.cfc 1235 2009-01-09 10:09:35Z 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. Uses the GenericChannelFitler for filtering. See that CFC for information on how to use to setup filters. Configuration Example: <property name="logging" type="MachII.properties.LoggingProperty"> <parameters> <parameter name="MachIILog"> <struct> <key name="type" value="MachII.logging.adapters.MachIILogAdapter" /> <!-- Optional and defaults to true /> <key name="loggingEnabled" value="true|false" /> <!-- Optional and defaults to 'info' --> <key name="loggingLevel" value="all|trace|debug|info|warn|error|fatal|off" /> <!-- Optional and defaults to false --> <key name="debugModeOnly" value="true|false" /> <!-- Optional --> <key name="filter" value="list,of,filter,criteria" /> - OR - <key name="filter"> <array> <element value="array" /> <element value="of" /> <element value="filter" /> <element value="criteria" /> </array> </key> </struct> </parameter> </parameters> </property> --->

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 string 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 struct getLoggingData()

Gets logging data. Call isLoggingDataDefined() first to check if defined.

public string getLoggingLevel()

Gets the logging level by name.

public string getLoggingPath()

Gets the logging path.

public string getLoggingScope()

Gets the logging scope.

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 isLoggingDataDefined()

Checks if logging data is defined.

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.

public void setLoggingLevel(string loggingLevelName)

Sets the logging level by name.

private void setLoggingPath(string loggingPath)

Sets the logging path.

private void setLoggingScope(string loggingScope)

Sets the logging scope.

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.

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("loggingScope")>
			<cfset setLoggingScope(getParameter("loggingScope")) />
		</cfif>
		<cfif isParameterDefined("loggingPath")>
			<cfset setLoggingPath(getParameter("loggingPath")) />
		</cfif>
		<cfif isParameterDefined("loggingLevel")>
			<cfset setLoggingLevel(getParameter("loggingLevel")) />
		</cfif>
		<cfif isParameterDefined("loggingEnabled")>
			<cfset setLoggingEnabled(getParameter("loggingEnabled")) />
		</cfif>
		<cfif isParameterDefined("debugModeOnly")>
			<cfset setDebugModeOnly(getParameter("debugModeOnly")) />
		</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 string getDebugModeOnly( )

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

Parameters:

Code:

	<cffunction name="getDebugModeOnly" access="public" returntype="string" 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> 

getLoggingData

public struct getLoggingData( )

Gets logging data. Call isLoggingDataDefined() first to check if defined.

Parameters:

Code:

	<cffunction name="getLoggingData" access="public" returntype="struct" output="false"
		hint="Gets logging data. Call isLoggingDataDefined() first to check if defined.">

		<cfset var scope = StructGet(getLoggingScope()) />
		
		<cfreturn scope[getLoggingPath()] />
	</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> 

getLoggingPath

public string getLoggingPath( )

Gets the logging path.

Parameters:

Code:

	<cffunction name="getLoggingPath" access="public" returntype="string" output="false"
		hint="Gets the logging path.">
		<cfreturn variables.instance.loggingPath />
	</cffunction> 

getLoggingScope

public string getLoggingScope( )

Gets the logging scope.

Parameters:

Code:

	<cffunction name="getLoggingScope" access="public" returntype="string" output="false"
		hint="Gets the logging scope.">
		<cfreturn variables.instance.loggingScope />
	</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> 

isLoggingDataDefined

public boolean isLoggingDataDefined( )

Checks if logging data is defined.

Parameters:

Code:

	<cffunction name="isLoggingDataDefined" access="public" returntype="boolean" output="false"
		hint="Checks if logging data is defined.">

		<cfset var scope = StructGet(getLoggingScope()) />
		
		<cfreturn StructKeyExists(scope, getLoggingPath()) />
	</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 entry = StructNew() />
		<cfset var scope = StructGet(getLoggingScope()) />
		
		
		<cfif NOT isFilterDefined() OR getFilter().decide(arguments)>		
			
			<cfif NOT IsDefined(getLoggingScope() & "." & getLoggingPath() & ".data")>
				<cfset scope[getLoggingPath()] = StructNew() />
				<cfset scope[getLoggingPath()].data = ArrayNew(1) />
			</cfif>
			
			<cfset entry.channel = arguments.channel />
			<cfset entry.logLevel = arguments.logLevel />
			<cfset entry.logLevelName = translateLevelToName(arguments.logLevel) />
			<cfset entry.message = arguments.message />
			<cfset entry.currentTick = getTickCount() />
			
			<cfif StructKeyExists(arguments, "additionalInformation")>
				<cfset entry.additionalInformation = arguments.additionalInformation />
			<cfelse>
				<cfset entry.additionalInformation = "" />
			</cfif>
			
			<cfset ArrayAppend(scope[getLoggingPath()].data, entry) />
		</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> 

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> 

setLoggingPath

private void setLoggingPath( string loggingPath )

Sets the logging path.

Parameters:
string loggingPath

Code:

	<cffunction name="setLoggingPath" access="private" returntype="void" output="false"
		hint="Sets the logging path.">
		<cfargument name="loggingPath" type="string" required="true" />
		<cfset variables.instance.loggingPath = arguments.loggingPath />
	</cffunction> 

setLoggingScope

private void setLoggingScope( string loggingScope )

Sets the logging scope.

Parameters:
string loggingScope

Code:

	<cffunction name="setLoggingScope" access="private" returntype="void" output="false"
		hint="Sets the logging scope.">
		<cfargument name="loggingScope" type="string" required="true" />
		<cfset variables.instance.loggingScope = arguments.loggingScope />
	</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> 

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>