| Package: MachII.logging |
| A factory that creates log instances. |
<!--- 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: LogFactory.cfc 1118 2008-10-22 19:46:33Z peterfarrell $ Created version: 1.6.0 Updated version: 1.6.0 Notes: Mach-II Logging is heavily based on Apache Commons Logging interface but is more flexible as it allows you attach multiple loggers at once. Thank you to the Apache project for the inspiration for our implementation. Log adapters must be stored as a struct so they can be passed by reference. Otherwise some logs are requested before adapters have been setup and will not log any messages since they do not have any adapters. Implementation Notes: * Channel names are not case-sensitive as the channel name is converted useable struct key first [Hash(UCase(arguments.channell))] ---> |
| Method Summary | |
|---|---|
| public LogFactory |
init()
Initializes the factory. |
| public void |
addLogAdapter(string logAdapterName, AbstractLogAdapter logAdapter)
Adds a log adapter. |
| private string |
createChannelHash(string channel)
Creates a channel hash. |
| public void |
disableLogging()
Disables logging. |
| public void |
enableLogging()
Enables logging. |
| private Log |
getFromCache(string channel)
Gets a log from the cache. |
| public Log |
getLog(string channel)
Gets a new log instance. Returns a cached instance if the channel already exists. |
| public struct |
getLogAdapters()
Returns the log adapters. |
| private boolean |
hasInCache(string channel)
Checks to see if a log is already in the cache. |
| private void |
putToCache(string channel, Log log)
Puts a log into the cache. |
| private void |
setLogAdapters(struct logAdapters)
Sets the log adapters. |
| Method Detail |
|---|
| addLogAdapter |
|---|
public void addLogAdapter( string logAdapterName, AbstractLogAdapter logAdapter )
Adds a log adapter.
Parameters:
| string logAdapterName |
| AbstractLogAdapter logAdapter |
Code:
<cffunction name="addLogAdapter" access="public" returntype="void" output="false" hint="Adds a log adapter."> <cfargument name="logAdapterName" type="string" required="true" /> <cfargument name="logAdapter" type="MachII.logging.adapters.AbstractLogAdapter" required="true" /> <cfset variables.logAdapters[arguments.logAdapterName] = arguments.logAdapter /> </cffunction>
| createChannelHash |
|---|
private string createChannelHash( string channel )
Creates a channel hash.
Parameters:
| string channel |
Code:
<cffunction name="createChannelHash" access="private" returntype="string" output="false" hint="Creates a channel hash."> <cfargument name="channel" type="string" required="true" /> <cfreturn Hash(UCase(arguments.channel)) /> </cffunction>
| disableLogging |
|---|
public void disableLogging( )
Disables logging.
Parameters:
Code:
<cffunction name="disableLogging" access="public" returntype="void" output="false" hint="Disables logging."> <cfset var key = "" /> <cfloop collection="#variables.logAdapters#" item="key"> <cfset variables.logAdapters[key].setLoggingEnabled(false) /> </cfloop> </cffunction>
| enableLogging |
|---|
public void enableLogging( )
Enables logging.
Parameters:
Code:
<cffunction name="enableLogging" access="public" returntype="void" output="false" hint="Enables logging."> <cfset var key = "" /> <cfloop collection="#variables.logAdapters#" item="key"> <cfset variables.logAdapters[key].setLoggingEnabled(true) /> </cfloop> </cffunction>
| getFromCache |
|---|
private Log getFromCache( string channel )
Gets a log from the cache.
Parameters:
| string channel |
Code:
<cffunction name="getFromCache" access="private" returntype="MachII.logging.Log" output="false" hint="Gets a log from the cache."> <cfargument name="channel" type="string" required="true" /> <cfreturn variables.logCache[createChannelHash(arguments.channel)] /> </cffunction>
| getLog |
|---|
public Log getLog( string channel )
Gets a new log instance. Returns a cached instance if the channel already exists.
Parameters:
| string channel |
Code:
<cffunction name="getLog" access="public" returntype="MachII.logging.Log" output="false"
hint="Gets a new log instance. Returns a cached instance if the channel already exists.">
<cfargument name="channel" type="string" required="true"
hint="Channel to log. Typically 'getMetadata(this).name'" />
<cfset var log = "" />
<cfset var channelHash = createChannelHash(arguments.channel) />
<cflock name="_MachIILogFactory.channel_#channelHash#" type="exclusive" timeout="10" throwontimeout="true">
<cfif hasInCache(arguments.channel)>
<cfset log = getFromCache(arguments.channel) />
<cfelse>
<cfset log = CreateObject("component", "MachII.logging.Log").init(arguments.channel, getLogAdapters()) />
<cfset putToCache(arguments.channel, log) />
</cfif>
</cflock>
<cfreturn log />
</cffunction>
| getLogAdapters |
|---|
public struct getLogAdapters( )
Returns the log adapters.
Parameters:
Code:
<cffunction name="getLogAdapters" access="public" returntype="struct" output="false" hint="Returns the log adapters."> <cfreturn variables.logAdapters /> </cffunction>
| hasInCache |
|---|
private boolean hasInCache( string channel )
Checks to see if a log is already in the cache.
Parameters:
| string channel |
Code:
<cffunction name="hasInCache" access="private" returntype="boolean" output="false" hint="Checks to see if a log is already in the cache."> <cfargument name="channel" type="string" required="true" /> <cfset var result = false /> <cfif StructKeyExists(variables.logCache, createChannelHash(arguments.channel))> <cfset result = true /> </cfif> <cfreturn result /> </cffunction>
| init |
|---|
public LogFactory init( )
Initializes the factory.
Parameters:
Code:
<cffunction name="init" access="public" returntype="LogFactory" output="false" hint="Initializes the factory."> <cfreturn this /> </cffunction>
| putToCache |
|---|
private void putToCache( string channel, Log log )
Puts a log into the cache.
Parameters:
| string channel |
| Log log |
Code:
<cffunction name="putToCache" access="private" returntype="void" output="false" hint="Puts a log into the cache."> <cfargument name="channel" type="string" required="true" /> <cfargument name="log" type="MachII.logging.Log" required="true" /> <cfset variables.logCache[createChannelHash(arguments.channel)] = arguments.log /> </cffunction>
| setLogAdapters |
|---|
private void setLogAdapters( struct logAdapters )
Sets the log adapters.
Parameters:
| struct logAdapters |
Code:
<cffunction name="setLogAdapters" access="private" returntype="void" output="false" hint="Sets the log adapters."> <cfargument name="logAdapters" type="struct" required="true" /> <cfset variables.logAdapters = arguments.logAdapters /> </cffunction>