Logger

Package: MachII.logging.loggers.EmailLog
Inherits from: logging.loggers.AbstractLogger
A logger for sending emails of logs.

<!--- 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: Logger.cfc 1118 2008-10-22 19:46:33Z peterfarrell $ Created version: 1.6.0 Updated version: 1.6.0 Notes: <property name="Logging" type="MachII.logging.LoggingProperty"> <parameters> <parameter name="EmailLog"> <struct> <key name="type" value="MachII.logging.loggers.EmailLog.Logger" /> <!-- Optional and defaults to true --> <key name="loggingEnabled" value="true|false" /> <!-- Optional and defaults to 'fatal' --> <key name="loggingLevel" value="all|trace|debug|info|warn|error|fatal|off" /> <!-- Optional and defaults to the default display template if not defined --> <key name="emailTemplateFile" value="/path/to/customEmailTemplate.cfm" /> <!-- Required - list of email addresses to send the log report to --> <key name="to" value="list,of,email,addresses" /> <!-- Required - email address to send the log report from --> <key name="from" value="logreports@example.com" /> <!-- Optional - the name of the subject of the log report email defaults to 'Application Log' --> <key name="subject" value="Application Log" /> <!-- Optional - list of mail server names or IPs defaults to mail server specified in the coldfusion admin --> <key name="servers" value="mail.example.com" /> <!-- 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> Uses the generic channel filter (MachII.logging.filters.GenericChannelFilter)for filtering. See that file header for configuration of filter criteria. --->

Method Summary
private array arrayConcat(array array1, array array2)

Concats two arrays together.

public void configure()

Configures the logger.

public struct getConfigurationData()

Gets the configuration data for this logger including adapter and filter.

public string getEmailTemplateFile()

Gets the email template location.

public string getFrom()
public string getServers()
public string getSubject()
public string getTo()
public void onRequestEnd()

Sends an email for this logger.

public void postRedirect(struct data)

Post-redirect logic for this logger.

public void preRedirect(struct data)

Pre-redirect logic for this logger.

private void setEmailTemplateFile(string emailTemplateFile)

Sets the email template location.

private void setFrom(string from)
private void setServers(string servers)
private void setSubject(string subject)
private void setTo(string to)
Methods inherited from logging.loggers.AbstractLogger:   setLoggingLevel , isParameterDefined , getLoggerTypeName , setParameter , setLoggingEnabled , init , isMethodDefined , isPrePostRedirectAvailable , getLogAdapter , getLoggerType , isLoggingEnabled , getParameterNames , isOnRequestEndAvailable , getParameter , setParameters , setLoggerId , getParameters , setLogAdapter , getLoggerId , getLoggingLevel
Method Detail
arrayConcat

private array arrayConcat( array array1, array array2 )

Concats two arrays together.

Parameters:
array array1
array array2

Code:

	<cffunction name="arrayConcat" access="private" returntype="array" output="false"
		hint="Concats two arrays together.">
		<cfargument name="array1" type="array" required="true" />
		<cfargument name="array2" type="array" required="true" />
		
		<cfset var result = arguments.array1 />
		<cfset var i = 0 />
		
		<cfloop from="1" to="#ArrayLen(arguments.array2)#" index="i">
			<cfset ArrayAppend(result, arguments.array2[i]) />
		</cfloop>
		
		<cfreturn result />
	</cffunction> 

configure

public void configure( )

Configures the logger.

Parameters:

Code:

	<cffunction name="configure" access="public" returntype="void" output="false"
		hint="Configures the logger.">
		
		<cfset var filter = CreateObject("component", "MachII.logging.filters.GenericChannelFilter").init(getParameter("filter", "")) />
		<cfset var adapter = CreateObject("component", "MachII.logging.adapters.ScopeAdapter").init(getParameters()) />
		
		
		<cfset adapter.setFilter(filter) />
		
		
		<cfset adapter.configure() />
		<cfset setLogAdapter(adapter) />
		
		
		<cfif isParameterDefined("emailTemplateFile")>
			<cfset setEmailTemplateFile(getParameter("emailTemplateFile")) />
		</cfif>
		
		<cfif isParameterDefined("to")>
			<cfset setTo(getParameter("to")) />
		<cfelse>
			<cfthrow type="MachII.logging.loggers.EmailLog.Logger"
				message="A parameter named 'to' is required. A list of email address(es) to send a log report to.">
		</cfif>
		
		<cfif isParameterDefined("from")>
			<cfset setFrom(getParameter("from")) />
		<cfelse>
			<cfthrow type="MachII.logging.loggers.EmailLog.Logger"
				message="A parameter named 'from' is required. This indicates the email address to send a log report from.">
		</cfif>
		
		<cfset setSubject(getParameter("subject", "Application Log")) />
		<cfset setServers(getParameter("servers", "")) />
	</cffunction> 

getConfigurationData

public struct getConfigurationData( )

Gets the configuration data for this logger including adapter and filter.

Parameters:

Code:

	<cffunction name="getConfigurationData" access="public" returntype="struct" output="false"
		hint="Gets the configuration data for this logger including adapter and filter.">
		
		<cfset var data = StructNew() />
		
		<cfset data["To Email"] = getTo() />
		<cfset data["From Email"] = getFrom() />
		<cfset data["Subject"] = getSubject() />
		<cfset data["SMTP Servers"] = getServers() />
		<cfset data["Email Template"] = getEmailTemplateFile() />
		<cfset data["Logging Enabled"] = YesNoFormat(isLoggingEnabled()) />
		
		<cfreturn data />
	</cffunction> 

getEmailTemplateFile

public string getEmailTemplateFile( )

Gets the email template location.

Parameters:

Code:

	<cffunction name="getEmailTemplateFile" access="public" returntype="string" output="false"
		hint="Gets the email template location.">
		<cfreturn variables.instance.emailTemplateFile />
	</cffunction> 

getFrom

public string getFrom( )

Parameters:

Code:

	<cffunction name="getFrom" access="public" returntype="string" output="false">
		<cfreturn variables.instance.from />
	</cffunction> 

getServers

public string getServers( )

Parameters:

Code:

	<cffunction name="getServers" access="public" returntype="string" output="false">
		<cfreturn variables.instance.servers />
	</cffunction> 

getSubject

public string getSubject( )

Parameters:

Code:

	<cffunction name="getSubject" access="public" returntype="string" output="false">
		<cfreturn variables.instance.subject />
	</cffunction> 

getTo

public string getTo( )

Parameters:

Code:

	<cffunction name="getTo" access="public" returntype="string" output="false">
		<cfreturn variables.instance.to />
	</cffunction> 

onRequestEnd

public void onRequestEnd( )

Sends an email for this logger.

Parameters:

Code:

	<cffunction name="onRequestEnd" access="public" returntype="void" output="false"
		hint="Sends an email for this logger.">
		
		<cfset var body = "" />
		<cfset var data = ArrayNew(1) />
		<cfset var local = StructNew() />
		
		
		<cfif getLogAdapter().getLoggingEnabled() AND getLogAdapter().isLoggingDataDefined()>
			
			<cfset data = getLogAdapter().getLoggingData().data />
			
			<cfif ArrayLen(data)>
				
				<cfsavecontent variable="body">
					<cfinclude template="#getEmailTemplateFile()#" />
				</cfsavecontent>
				
				
				<cfif NOT Len(getServers())>
					<cfmail from="#getFrom()#" to="#getTo()#" subject="#getSubject()#"><cfoutput>#body#</cfoutput></cfmail>
				<cfelse>
					<cfmail from="#getFrom()#" to="#getTo()#" subject="#getSubject()#" server="#getServers()#"><cfoutput>#body#</cfoutput></cfmail>
				</cfif>
			</cfif>
		</cfif>
	</cffunction> 

postRedirect

public void postRedirect( struct data )

Post-redirect logic for this logger.

Parameters:
struct data

Code:

	<cffunction name="postRedirect" access="public" returntype="void" output="false"
		hint="Post-redirect logic for this logger.">
		<cfargument name="data" type="struct" required="true"
			hint="Redirect persist data struct." />

		<cfset var loggingData = StructNew() />
		
		<cfif getLogAdapter().getLoggingEnabled() AND getLogAdapter().isLoggingDataDefined()>
			<cftry>
				<cfset loggingData = getLogAdapter().getLoggingData() />
				<cfset loggingData.data = arrayConcat(arguments.data[getLoggerId()].data, loggingData.data) />
				<cfcatch type="any">
					
				</cfcatch>
			</cftry>
		</cfif>
	</cffunction> 

preRedirect

public void preRedirect( struct data )

Pre-redirect logic for this logger.

Parameters:
struct data

Code:

	<cffunction name="preRedirect" access="public" returntype="void" output="false"
		hint="Pre-redirect logic for this logger.">
		<cfargument name="data" type="struct" required="true"
			hint="Redirect persist data struct." />
		
		<cfif getLogAdapter().getLoggingEnabled() AND getLogAdapter().isLoggingDataDefined()>
			<cfset arguments.data[getLoggerId()] = getLogAdapter().getLoggingData() />
		</cfif>
	</cffunction> 

setEmailTemplateFile

private void setEmailTemplateFile( string emailTemplateFile )

Sets the email template location.

Parameters:
string emailTemplateFile

Code:

	<cffunction name="setEmailTemplateFile" access="private" returntype="void" output="false"
		hint="Sets the email template location.">
		<cfargument name="emailTemplateFile" type="string" required="true" />
		<cfset variables.instance.emailTemplateFile = arguments.emailTemplateFile />
	</cffunction> 

setFrom

private void setFrom( string from )

Parameters:
string from

Code:

	<cffunction name="setFrom" access="private" returntype="void" output="false">
		<cfargument name="from" type="string" required="true" />
		<cfset variables.instance.from = arguments.from />
	</cffunction> 

setServers

private void setServers( string servers )

Parameters:
string servers

Code:

	<cffunction name="setServers" access="private" returntype="void" output="false">
		<cfargument name="servers" type="string" required="true" />
		<cfset variables.instance.servers = arguments.servers />
	</cffunction> 

setSubject

private void setSubject( string subject )

Parameters:
string subject

Code:

	<cffunction name="setSubject" access="private" returntype="void" output="false">
		<cfargument name="subject" type="string" required="true" />
		<cfset variables.instance.subject = arguments.subject />
	</cffunction> 

setTo

private void setTo( string to )

Parameters:
string to

Code:

	<cffunction name="setTo" access="private" returntype="void" output="false">
		<cfargument name="to" type="string" required="true" />
		<cfset variables.instance.to = arguments.to />
	</cffunction>