Como fazer debug dos eventos do Application.cfc

10 views
Skip to first unread message

Christian Figueiredo

unread,
May 13, 2021, 10:40:16 AM5/13/21
to ColdFusion Brasil
Blz pessoal?

Estou compartilhando a função onError que criei para gerar arquivos HTML de debug em uma pasta específica.

Você pode pensar que vale mais a pena receber um e-mail quando um erro ocorrer. Mas você já pensou na possibilidade do servidor de e-mails parar?

public void function onError(required any exception,required string eventName){

    local['s_event']={

        'onApplicationStart'='as'

    ,   'onRequestStart'='rs'

    ,   'onRESTRequest'='rr'

    ,   'onPageLoading'='pl'

    ,   'onRequestEnd'='re'

    ,   'onAbort'='ab'

    ,   'onApplicationEnd'='ae'

    ,   'onError'='er'

    };

    arguments['eventName']=((yesNoFormat(len(trim(arguments['eventName']))))?(arguments['eventName']):('onPageLoading'));

    local['s_timeZone']={

        'v_UTC'=duplicate(getTimeZoneInfo()['utcTotalOffset'])

    ,   'v_local'=duplicate(getTimeZoneInfo()['utcTotalOffset'])*(-1)

    };

    local['v_folder']=expandPath('/')&'debug\'&'_'&dateFormat(dateAdd('s',local['s_timeZone']['v_UTC'],now()),'yyyy-mm-dd');

    if(not(directoryExists(local['v_folder']))){

        directoryCreate(local['v_folder']);

    }

    local['q_listErrorFiles']=directoryList(path=local['v_folder'],listInfo='query');

    local['v_file']=timeFormat(dateAdd('s',local['s_timeZone']['v_UTC'],now()),'HH-mm-ss-lll');

    local['v_simultaneous']=0;

    local['s_query']={

        'v_sql'="

            select

                1

            from [local].[q_listErrorFiles]

            where name=:v_errorFileName

            ;"

    ,   's_params'={

            'v_errorFileName'={

                'cfsqltype'='cf_sql_varchar'

            ,   'value'='_'&local['v_file']&toString(numberFormat(local['v_simultaneous'],'000'))&'_'&local['s_event'][arguments['eventName']]&'.html'

            }

        }

    };

    local['q_getErrorFile']=queryExecute(sql=local['s_query']['v_sql'],params=local['s_query']['s_params'],options={'DBType'='query'});

    while(local['q_getErrorFile'].recordCount>0){

        local['v_simultaneous']=local['v_simultaneous']+1;

        local['s_query']={

            'v_sql'="

                select

                    1

                from [local].[q_listErrorFiles]

                where name=:v_errorFileName

                ;"

        ,   's_params'={

                'v_errorFileName'={

                    'cfsqltype'='cf_sql_varchar'

                ,   'value'='_'&local['v_file']&toString(numberFormat(local['v_simultaneous'],'000'))&'_'&local['s_event'][arguments['eventName']]&'.html'

                }

            }

        };

        local['q_getErrorFile']=queryExecute(sql=local['s_query']['v_sql'],params=local['s_query']['s_params'],options={'DBType'='query'});

    }

    local['v_file']=local['v_folder']&'\'&'_'&local['v_file']&toString(numberFormat(local['v_simultaneous'],'000'))&'_'&local['s_event'][arguments['eventName']]&'.html';

    fileWrite(local['v_file'],'','utf-8');

    writeDump(var=getHTTPRequestData(),label='getHTTPRequestData()',format='html',output=local['v_file']);

    writeDump(var=arguments,label='arguments',format='html',output=local['v_file']);

    if(isDefined('variables')){

        writeDump(var=variables,label='variables',format='html',output=local['v_file']);

    }

    writeDump(var=cgi,label='cgi',format='html',output=local['v_file']);

    writeDump(var=server,label='server',format='html',output=local['v_file']);

    if(isDefined('application')){

        writeDump(var=application,label='application',format='html',output=local['v_file']);

    }

    writeDump(var=cookie,label='cookie',format='html',output=local['v_file']);

    writeDump(var=url,label='url',format='html',output=local['v_file']);

    if(isDefined('form')){

        writeDump(var=form,label='form',format='html',output=local['v_file']);

    }

    return;

}

Essa função gera arquivos HTML em uma pasta debug do seu site com o dia e a hora certa, convertido para GMT+00:00 (fuso-horário de Grenwich). Você pode personalizar seu diretório na linha 17. Se quiser uma função para o seu horário local, segue o código!

public void function onError(required any exception,required string eventName){

    local['s_event']={

        'onApplicationStart'='as'

    ,   'onRequestStart'='rs'

    ,   'onRESTRequest'='rr'

    ,   'onPageLoading'='pl'

    ,   'onRequestEnd'='re'

    ,   'onAbort'='ab'

    ,   'onApplicationEnd'='ae'

    ,   'onError'='er'

    };

    arguments['eventName']=((yesNoFormat(len(trim(arguments['eventName']))))?(arguments['eventName']):('onPageLoading'));

    local['v_folder']=expandPath('/')&'debug\'&'_'&dateFormat(now(),'yyyy-mm-dd');

    if(not(directoryExists(local['v_folder']))){

        directoryCreate(local['v_folder']);

    }

    local['q_listErrorFiles']=directoryList(path=local['v_folder'],listInfo='query');

    local['v_file']=timeFormat(now(),'HH-mm-ss-lll');

    local['v_simultaneous']=0;

    local['s_query']={

        'v_sql'="

            select

                1

            from [local].[q_listErrorFiles]

            where name=:v_errorFileName

            ;"

    ,   's_params'={

            'v_errorFileName'={

                'cfsqltype'='cf_sql_varchar'

            ,   'value'='_'&local['v_file']&toString(numberFormat(local['v_simultaneous'],'000'))&'_'&local['s_event'][arguments['eventName']]&'.html'

            }

        }

    };

    local['q_getErrorFile']=queryExecute(sql=local['s_query']['v_sql'],params=local['s_query']['s_params'],options={'DBType'='query'});

    while(local['q_getErrorFile'].recordCount>0){

        local['v_simultaneous']=local['v_simultaneous']+1;

        local['s_query']={

            'v_sql'="

                select

                    1

                from [local].[q_listErrorFiles]

                where name=:v_errorFileName

                ;"

        ,   's_params'={

                'v_errorFileName'={

                    'cfsqltype'='cf_sql_varchar'

                ,   'value'='_'&local['v_file']&toString(numberFormat(local['v_simultaneous'],'000'))&'_'&local['s_event'][arguments['eventName']]&'.html'

                }

            }

        };

        local['q_getErrorFile']=queryExecute(sql=local['s_query']['v_sql'],params=local['s_query']['s_params'],options={'DBType'='query'});

    }

    local['v_file']=local['v_folder']&'\'&'_'&local['v_file']&toString(numberFormat(local['v_simultaneous'],'000'))&'_'&local['s_event'][arguments['eventName']]&'.html';

    fileWrite(local['v_file'],'','utf-8');

    writeDump(var=getHTTPRequestData(),label='getHTTPRequestData()',format='html',output=local['v_file']);

    writeDump(var=arguments,label='arguments',format='html',output=local['v_file']);

    if(isDefined('variables')){

        writeDump(var=variables,label='variables',format='html',output=local['v_file']);

    }

    writeDump(var=cgi,label='cgi',format='html',output=local['v_file']);

    writeDump(var=server,label='server',format='html',output=local['v_file']);

    if(isDefined('application')){

        writeDump(var=application,label='application',format='html',output=local['v_file']);

    }

    writeDump(var=cookie,label='cookie',format='html',output=local['v_file']);

    writeDump(var=url,label='url',format='html',output=local['v_file']);

    if(isDefined('form')){

        writeDump(var=form,label='form',format='html',output=local['v_file']);

    }

    return;

}

 

Juntando um com o outro, você pode incluir a linha de debug no evento onRESTRequest:

public any function onRESTRequest(required string cfcname,required string method,required struct args){

    local['s_return']=null;

    cfinvoke(component=arguments.cfcname,method=arguments.method,argumentCollection=arguments.args,returnVariable='local.s_return');

    onError(exception=duplicate(local),eventName='onRESTRequest');

    return local['s_return'];

}

Espero que essa função te ajude no debug! Deus abençoe nós CFers!
Reply all
Reply to author
Forward
0 new messages