forked from tmuth/Logger---A-PL-SQL-Logging-Utility
-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathlogger.pkb
More file actions
3495 lines (3160 loc) · 114 KB
/
logger.pkb
File metadata and controls
3495 lines (3160 loc) · 114 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
create or replace package body logger
as
-- Note: The license is defined in the package specification of the logger package
--
-- Conditional Compilation variables:
-- $$NO_OP
-- When true, completely disables all logger DML.Also used to generate the logger_no_op.sql file
--
-- $$RAC_LT_11_2:
-- Set in logger_configure.
-- Handles the fact that RAC doesn't support global app contexts until 11.2
--
-- $$FLASHBACK_ENABLED
-- Set in logger_configure.
-- Determine whether or not we can grab the scn from dbms_flashback.
-- Primarily used in the trigger on logger_logs.
--
-- $$APEX
-- Set in logger_configure.
-- True if we can query a local synonym to wwv_flow_data to snapshot the APEX session state.
--
-- $$LOGGER_DEBUG
-- Only to be used during development of logger
-- Primarily used for dbms_output.put_line calls
-- Part of #64
--
-- $$LOGGER_PLUGIN_<TYPE> : For each type of plugin
-- Introduced with #46
-- $$LOGGER_PLUGIN_ERROR
--
-- $$LOGGER_CONTEXT
-- #82
-- If true, context is used for values preferences. False we read from table each time
-- TYPES
type ts_array is table of timestamp index by varchar2(100);
-- VARIABLES
g_log_id number;
g_proc_start_times ts_array;
g_running_timers pls_integer := 0;
-- #46
g_plug_logger_log_error rec_logger_log;
g_in_plugin_error boolean := false;
-- CONSTANTS
gc_line_feed constant varchar2(1) := chr(10);
gc_cflf constant varchar2(2) := chr(13)||chr(10);
gc_date_format constant varchar2(255) := 'DD-MON-YYYY HH24:MI:SS';
gc_timestamp_format constant varchar2(255) := gc_date_format || ':FF';
gc_timestamp_tz_format constant varchar2(255) := gc_timestamp_format || ' TZR';
gc_ctx_attr_level constant varchar2(5) := 'level';
gc_ctx_attr_include_call_stack constant varchar2(18) := 'include_call_stack';
-- #46 Plugin context names
gc_ctx_plugin_fn_log constant varchar2(30) := 'plugin_fn_log';
gc_ctx_plugin_fn_info constant varchar2(30) := 'plugin_fn_information';
gc_ctx_plugin_fn_warn constant varchar2(30) := 'plugin_fn_warning';
gc_ctx_plugin_fn_error constant varchar2(30) := 'plugin_fn_error';
gc_ctx_plugin_fn_perm constant varchar2(30) := 'plugin_fn_permanent';
-- #113 Preference names
gc_pref_level constant logger_prefs.pref_name%type := 'LEVEL';
gc_pref_include_call_stack constant logger_prefs.pref_name%type := 'INCLUDE_CALL_STACK';
gc_pref_protect_admin_procs constant logger_prefs.pref_name%type := 'PROTECT_ADMIN_PROCS';
gc_pref_install_schema constant logger_prefs.pref_name%type := 'INSTALL_SCHEMA';
gc_pref_purge_after_days constant logger_prefs.pref_name%type := 'PURGE_AFTER_DAYS';
gc_pref_purge_min_level constant logger_prefs.pref_name%type := 'PURGE_MIN_LEVEL';
gc_pref_logger_version constant logger_prefs.pref_name%type := 'LOGGER_VERSION';
gc_pref_client_id_expire_hours constant logger_prefs.pref_name%type := 'PREF_BY_CLIENT_ID_EXPIRE_HOURS';
gc_pref_logger_debug constant logger_prefs.pref_name%type := 'LOGGER_DEBUG';
gc_pref_plugin_fn_error constant logger_prefs.pref_name%type := 'PLUGIN_FN_ERROR';
gc_pref_global_context_name constant logger_prefs.pref_name%type := 'GLOBAL_CONTEXT_NAME';
-- *** PRIVATE ***
/**
* @private
*
* @author Martin D'Souza
* @created
* @param p_str String to convert to number
* @return True if p_str is a number
*/
function is_number(p_str in varchar2)
return boolean
as
l_num number;
begin
l_num := to_number(p_str);
return true;
exception
when others then
return false;
end is_number;
/**
* @private
*
* Validates assertion. Will raise an application error if assertion is false
*
* @author Martin D'Souza
* @created 29-Mar-2013
* @param p_condition Boolean condition to validate
* @param p_message Message to include in application error if p_condition fails
*/
procedure assert(
p_condition in boolean,
p_message in varchar2)
as
begin
$if $$no_op $then
null;
$else
if not p_condition or p_condition is null then
raise_application_error(-20000, p_message);
end if;
$end
end assert;
/**
* @private
* Returns the display/print friendly parameter information
*
* @author Martin D'Souza
* @created 20-Jan-2013
* @param p_parms Array of parameters (can be null)
* @return Clob of param information
*/
function get_param_clob(p_params in logger.tab_param)
return clob
as
l_return clob;
l_no_vars constant varchar2(255) := 'No params defined';
l_index pls_integer;
begin
$if $$no_op $then
return null;
$else
-- Generate line feed delimited list
if p_params.count > 0 then
-- Using while true ... option allows for unordered param list
l_index := p_params.first;
while true loop
l_return := l_return || p_params(l_index).name || ': ' || p_params(l_index).val;
l_index := p_params.next(l_index);
if l_index is null then
exit;
else
l_return := l_return || gc_line_feed;
end if;
end loop;
else
-- No Parameters
l_return := l_no_vars;
end if;
return l_return;
$end
end get_param_clob;
/**
* @private
* Sets the global context
*
* @author Tyler Muth
* @created ???
* @param p_attribute Attribute for context to set
* @param p_value Value
* @param p_client_id Optional client_id. If specified will only set the attribute/value for specific client_id (not global)
*/
procedure save_global_context(
p_attribute in varchar2,
p_value in varchar2,
p_client_id in varchar2 default null)
is
pragma autonomous_transaction;
begin
$if $$no_op $then
null;
$else
dbms_session.set_context(
namespace => g_context_name,
attribute => p_attribute,
value => p_value,
client_id => p_client_id);
$end
commit; -- MD: moved commit to outside of the NO_OP check since commit or rollback must occur in this procedure
exception
when others then
-- #128 Error ORA-01031: insufficient privileges will be raised when the context is missing
-- This usually happens whent the schema has been copied or database replicated
if sqlcode = -01031 then
raise_application_error(-20001, logger.sprintf(q'!
Context %s1 is missing (usually due to cloned shema or database).
Run: "create or replace context %s1 using logger accessed globally;" to recreate it
See https://github.com/OraOpenSource/Logger/issues/128 for more info!',
logger.g_context_name));
end if;
end save_global_context;
/**
* @private
* Returns the extra column appended with the display friendly parameters
*
* @author Martin D'Souza
* @created 1-May-2013
* @param p_extra Current "Extra" field
* @param p_params Parameters. If null, then no changes to the Extra column
*/
function set_extra_with_params(
p_extra in logger_logs.extra%type,
p_params in tab_param
)
return logger_logs.extra%type
as
l_extra logger_logs.extra%type;
begin
$if $$no_op $then
return null;
$else
if p_params.count = 0 then
return p_extra;
else
l_extra := p_extra || gc_line_feed || gc_line_feed || '*** Parameters ***' || gc_line_feed || gc_line_feed || get_param_clob(p_params => p_params);
end if;
return l_extra;
$end
end set_extra_with_params;
/**
* @private
* Returns common system level context values
*
* @author Tyler Muth
* @created ???
* @param p_detail_level USER, ALL, NLS, USER, or INSTANCe
* @param p_vertical True of values should have a line break after each value. False for comman seperated list.
* @param p_show_null Show null values
* @return
*/
function get_sys_context(
p_detail_level in varchar2 default 'USER', -- ALL, NLS, USER, INSTANCE
p_vertical in boolean default false,
p_show_null in boolean default false)
return clob
is
l_ctx clob;
l_detail_level varchar2(20) := upper(p_detail_level);
$if $$no_op $then
$else
procedure append_ctx(p_name in varchar2)
is
r_pad number := 30;
l_value varchar2(100);
invalid_userenv_parm exception;
pragma exception_init(invalid_userenv_parm, -2003);
begin
l_value := sys_context('USERENV',p_name);
if p_show_null or l_value is not null then
if p_vertical then
l_ctx := l_ctx || rpad(p_name,r_pad,' ')||': '|| l_value || gc_cflf;
else
l_ctx := l_ctx || p_name||': '|| l_value ||', ';
end if;
end if;
exception
when invalid_userenv_parm then
null;
end append_ctx;
$end
begin
$if $$no_op $then
return null;
$else
if l_detail_level in ('ALL','NLS','INSTANCE') then
append_ctx('NLS_CALENDAR');
append_ctx('NLS_CURRENCY');
append_ctx('NLS_DATE_FORMAT');
append_ctx('NLS_DATE_LANGUAGE');
append_ctx('NLS_SORT');
append_ctx('NLS_TERRITORY');
append_ctx('LANG');
append_ctx('LANGUAGE');
end if;
if l_detail_level in ('ALL','USER') then
append_ctx('CURRENT_SCHEMA');
append_ctx('SESSION_USER');
append_ctx('OS_USER');
append_ctx('CLIENT_IDENTIFIER');
append_ctx('CLIENT_INFO');
append_ctx('IP_ADDRESS');
append_ctx('HOST');
append_ctx('TERMINAL');
end if;
if l_detail_level in ('ALL','USER') then
append_ctx('AUTHENTICATED_IDENTITY');
append_ctx('AUTHENTICATION_DATA');
append_ctx('AUTHENTICATION_METHOD');
append_ctx('ENTERPRISE_IDENTITY');
append_ctx('POLICY_INVOKER');
append_ctx('PROXY_ENTERPRISE_IDENTITY');
append_ctx('PROXY_GLOBAL_UID');
append_ctx('PROXY_USER');
append_ctx('PROXY_USERID');
append_ctx('IDENTIFICATION_TYPE');
append_ctx('ISDBA');
end if;
if l_detail_level in ('ALL','INSTANCE') then
append_ctx('DB_DOMAIN');
append_ctx('DB_NAME');
append_ctx('DB_UNIQUE_NAME');
append_ctx('INSTANCE');
append_ctx('INSTANCE_NAME');
append_ctx('SERVER_HOST');
append_ctx('SERVICE_NAME');
end if;
if l_detail_level in ('ALL') then
append_ctx('ACTION');
append_ctx('AUDITED_CURSORID');
append_ctx('BG_JOB_ID');
append_ctx('CURRENT_BIND');
append_ctx('CURRENT_SCHEMAID');
append_ctx('CURRENT_SQL');
append_ctx('CURRENT_SQLn');
append_ctx('CURRENT_SQL_LENGTH');
append_ctx('ENTRYID');
append_ctx('FG_JOB_ID');
append_ctx('GLOBAL_CONTEXT_MEMORY');
append_ctx('GLOBAL_UID');
append_ctx('MODULE');
append_ctx('NETWORK_PROTOCOL');
append_ctx('SESSION_USERID');
append_ctx('SESSIONID');
append_ctx('SID');
append_ctx('STATEMENTID');
end if;
return rtrim(l_ctx,', ');
$end
end get_sys_context;
/**
* @private
* Checks if admin functions can be run
*
* @author Tyler Muth
* @created ???
* @return True if user can run admin procs.
*/
function admin_security_check
return boolean
is
l_protect_admin_procs logger_prefs.pref_value%type;
l_return boolean default false;
begin
$if $$no_op $then
l_return := true;
$else
l_protect_admin_procs := get_pref(logger.gc_pref_protect_admin_procs);
if l_protect_admin_procs = 'TRUE' then
if get_pref(logger.gc_pref_install_schema) = sys_context('USERENV','SESSION_USER') then
l_return := true;
else
l_return := false;
raise_application_error (-20000, 'You are not authorized to call this procedure. Change Logger pref: PROTECT_ADMIN_PROCS to FALSE to avoid this.');
end if;
else
l_return := true;
end if;
$end
return l_return;
end admin_security_check;
/**
* @private
*
* @issue #111 Use get_pref to remove duplicate code
*
* @author Tyler Muth
* @created ???
* @return If client level specified will return it, otherwise global level.
*/
function get_level_number
return number
$if $$rac_lt_11_2 or not $$logger_context $then -- TODO mdsouza: I think we can remove this as all we care about is db_version
$if not dbms_db_version.ver_le_10_2 $then
result_cache relies_on (logger_prefs, logger_prefs_by_client_id)
$end
$end
is
l_level number;
l_level_char varchar2(50);
$if $$logger_debug $then
l_scope varchar2(30) := 'get_level_number';
$end
begin
$if $$no_op $then
return g_off;
$else
$if $$logger_debug $then
dbms_output.put_line(l_scope || ': selecting logger_level');
$end
l_level := convert_level_char_to_num(logger.get_pref(logger.gc_pref_level));
return l_level;
$end
end get_level_number;
/**
* @private
*
* @author Tyler Muth
* @created ???
* @return true if call stack should be returned when logging
*/
function include_call_stack
return boolean
$if 1=1
and ($$rac_lt_11_2 or not $$logger_context)
and not dbms_db_version.ver_le_10_2
and ($$no_op is null or not $$no_op) $then
result_cache relies_on (logger_prefs, logger_prefs_by_client_id)
$end
is
l_call_stack_pref logger_prefs.pref_value%type;
begin
$if $$no_op $then
return false;
$else
$if $$rac_lt_11_2 or not $$logger_context $then
l_call_stack_pref := get_pref(logger.gc_pref_include_call_stack);
$else
l_call_stack_pref := sys_context(g_context_name,gc_ctx_attr_include_call_stack);
if l_call_stack_pref is null then
l_call_stack_pref := get_pref(logger.gc_pref_include_call_stack);
save_global_context(
p_attribute => gc_ctx_attr_include_call_stack,
p_value => l_call_stack_pref,
p_client_id => sys_context('userenv','client_identifier'));
end if;
$end
if l_call_stack_pref = 'TRUE' then
return true;
else
return false;
end if;
$end
end include_call_stack;
/**
* @private
* Returns date diff in "... sectons/minutes/days/etc ago" format
*
* @author Tyler Muth
* @created ???
* @param p_date_start
* @param p_date_stop
* @return Text version of date diff
*/
function date_text_format_base (
p_date_start in date,
p_date_stop in date)
return varchar2
as
x varchar2(20);
begin
$if $$no_op $then
return null;
$else
x :=
case
when p_date_stop-p_date_start < 1/1440
then round(24*60*60*(p_date_stop-p_date_start)) || ' seconds'
when p_date_stop-p_date_start < 1/24
then round(24*60*(p_date_stop-p_date_start)) || ' minutes'
when p_date_stop-p_date_start < 1
then round(24*(p_date_stop-p_date_start)) || ' hours'
when p_date_stop-p_date_start < 14
then trunc(p_date_stop-p_date_start) || ' days'
when p_date_stop-p_date_start < 60
then trunc((p_date_stop-p_date_start)/7) || ' weeks'
when p_date_stop-p_date_start < 365
then round(months_between(p_date_stop,p_date_start)) || ' months'
else round(months_between(p_date_stop,p_date_start)/12,1) || ' years'
end;
x:= regexp_replace(x,'(^1 [[:alnum:]]{4,10})s','\1');
x:= x || ' ago';
return substr(x,1,20);
$end
end date_text_format_base;
/**
* @private
*
* Parses the callstack to get unit and line number
*
* @author Tyler Muth
* @created ???
* @param p_callstack
* @param o_unit
* @param o_lineno
*/
procedure get_debug_info(
p_callstack in clob default null,
o_unit out varchar2,
o_lineno out varchar2 )
as
l_callstack varchar2(10000) := p_callstack;
l_sub_program utl_call_stack.unit_qualified_name;
l_current_unit varchar2(257);
l_log_package_name varchar2(128) := $$PLSQL_UNIT_OWNER || '.' || $$PLSQL_UNIT;
l_level pls_integer := 2; -- offset to save internal iterations
function get_package_name(p_unit_name in varchar2) return varchar2 is
begin
return substr(p_unit_name, 1, instr(p_unit_name, '.') - 1);
end get_package_name;
begin
$if $$no_op $then
null;
$else
$if dbms_db_version.ver_le_11_2 $then
l_callstack := substr( l_callstack, instr( l_callstack, chr(10), 1, 5 )+1 );
l_callstack := substr( l_callstack, 1, instr( l_callstack, chr(10), 1, 1 )-1 );
l_callstack := trim( substr( l_callstack, instr( l_callstack, ' ' ) ) );
o_lineno := substr( l_callstack, 1, instr( l_callstack, ' ' )-1 );
o_unit := trim(substr( l_callstack, instr( l_callstack, ' ', -1, 1 ) ));
$else
--2019-01-23 Vidar Eidissen: catching first package outside the logger-package as the calling
l_current_unit := l_log_package_name;
while l_level < utl_call_stack.dynamic_depth and l_current_unit = l_log_package_name loop
l_level := l_level + 1;
l_sub_program := UTL_CALL_STACK.SUBPROGRAM(l_level);
l_current_unit := utl_call_stack.owner(l_level) || '.' || get_package_name(utl_call_stack.concatenate_subprogram(l_sub_program));
end loop;
o_unit := utl_call_stack.owner(l_level) || '.' || utl_call_stack.concatenate_subprogram(l_sub_program);
o_lineno := utl_call_stack.unit_line(l_level);
$end
$end
end get_debug_info;
/**
* @private
* Main procedure that will store log data into logger_logs table
*
* Modifications
* - 2.1.0: If text is > 4000 characters, it will be moved to the EXTRA column
*
* @author Tyler Muth
* @created ???
* @param p_text
* @param p_log_level
* @param p_scope
* @param p_extra
* @param p_callstack
* @param p_params
*/
procedure log_internal(
p_text in varchar2,
p_log_level in number,
p_scope in varchar2,
p_extra in clob default null,
p_callstack in varchar2 default null,
p_params in tab_param default logger.gc_empty_tab_param)
is
l_proc_name varchar2(100);
l_lineno varchar2(100);
l_text varchar2(32767);
l_callstack varchar2(3000);
l_extra logger_logs.extra%type;
begin
$if $$no_op $then
null;
$else
l_text := p_text;
-- Generate callstack text
if p_callstack is not null and logger.include_call_stack then
$if dbms_db_version.ver_le_11_2 $then
logger.get_debug_info(
p_callstack => p_callstack,
o_unit => l_proc_name,
o_lineno => l_lineno);
$else
--2019-01-23 Vidar Eidissen: don't need to pass the call stack to get the unit and line from 12c
logger.get_debug_info(
o_unit => l_proc_name,
o_lineno => l_lineno);
$end
l_callstack := regexp_replace(p_callstack,'^.*$','',1,4,'m');
l_callstack := regexp_replace(l_callstack,'^.*$','',1,1,'m');
l_callstack := ltrim(replace(l_callstack,chr(10)||chr(10),chr(10)),chr(10));
end if;
l_extra := set_extra_with_params(p_extra => p_extra, p_params => p_params);
ins_logger_logs(
p_unit_name => upper(l_proc_name) ,
p_scope => p_scope ,
p_logger_level =>p_log_level,
p_extra => l_extra,
p_text =>l_text,
p_call_stack =>l_callstack,
p_line_no => l_lineno,
po_id => g_log_id);
$end
end log_internal;
/**
* @private
* Run plugin
*
* Notes:
* - Currently only supports error type plugin but has been built to support other types
* - -- FUTURE mdsouza: When supporting other plugin types put conditional compilation where applicable
* - -- FUTURE mdsouza: Include this in tests (#86)
*
* @issue #46
*
* @author Martin D'Souza
* @created 11-Mar-2015
* @param p_logger_log Record that plugin should be run for
*/
procedure run_plugin(p_logger_log in logger.rec_logger_log)
as
l_plugin_fn logger_prefs.pref_value%type;
l_plugin_ctx varchar2(30);
l_sql varchar2(255);
-- For exception block
l_params logger.tab_param;
l_scope logger_logs.scope%type;
-- Mark "in_plugin" as true/false
-- Put in separate procedure since more logic may be applied
-- And called from exception block as well
procedure start_stop_plugin(
p_in_plugin boolean -- True/False depending on action
)
as
begin
if p_logger_log.logger_level = logger.g_error then
g_in_plugin_error := p_in_plugin;
end if;
end start_stop_plugin;
function f_get_set_global_context(
p_ctx in varchar2
)
return varchar2
as
l_return varchar2(255);
begin
$if $$logger_debug $then
dbms_output.put_line('Calling f_get_set_global_conext');
$end
l_return := upper(get_pref(p_pref_name =>
case
when p_logger_log.logger_level = g_error then gc_ctx_plugin_fn_error
end
));
$if $$logger_debug $then
dbms_output.put_line('l_return: ' || l_return);
$end
save_global_context(p_attribute => p_ctx, p_value => l_return);
return l_return;
end f_get_set_global_context;
begin
$if $$no_op $then
null;
$else
start_stop_plugin(p_in_plugin => true);
$if $$logger_debug $then
dbms_output.put_line('in run_plugin. g_in_plugin_error: ' || logger.tochar(g_in_plugin_error));
$end
if 1=2 then
null;
elsif p_logger_log.logger_level = logger.g_error then
l_plugin_ctx := gc_ctx_plugin_fn_error;
end if;
if l_plugin_ctx is not null then
l_plugin_fn := coalesce(
sys_context(g_context_name, l_plugin_ctx),
f_get_set_global_context(p_ctx => l_plugin_ctx));
$if $$logger_debug $then
dbms_output.put_line('l_plugin_fn: ' || l_plugin_fn);
$end
if 1=1
and l_plugin_fn is not null
and l_plugin_fn != 'NONE' then
l_sql := 'begin ' || l_plugin_fn || '(logger.get_plugin_rec(' || p_logger_log.logger_level || ')); end;';
$if $$logger_debug $then
dbms_output.put_line('l_sql: ' || l_sql);
$end
execute immediate l_sql;
else
-- Should never reach this point since plugin_fn should have a value
logger.log_error('Error l_plugin_fn does not have value');
end if; -- l_plugin_fn
else
-- Should never reach this point since plugin_ctx should have a value
logger.log_error('Error l_plugin_ctx does not have value');
end if; -- l_plugin_ctx is not null
start_stop_plugin(p_in_plugin => false);
$end
exception
when others then
logger.append_param(l_params, 'Logger.id', p_logger_log.id);
logger.append_param(l_params, 'Logger.logger_level', p_logger_log.logger_level);
logger.append_param(l_params, 'Plugin Function', l_plugin_fn);
select scope
into l_scope
from logger_logs_5_min
where 1=1
and id = p_logger_log.id;
logger.log_error('Exception in plugin procedure: ' || l_plugin_fn, l_scope, null, l_params);
start_stop_plugin(p_in_plugin => false);
raise;
end run_plugin;
/**
* @private
* Store APEX items in logger_logs_apex_items
*
* @issue #115: Only log not-null values
* @issue #114: Bulk insert (no more row by row)
* @issue #54: Support for p_item_type
*
* @author Tyler Muth
* @created ???
* @param p_log_id logger_logs.id to reference
* @param p_item_type Either the `g_apex_item_type_...` type or just the APEX page number for a specific page. It is assumed that it has been validated by the time it hits here.
* @param p_log_null_items If set to false, null values won't be logged
*/
procedure snapshot_apex_items(
p_log_id in logger_logs.id%type,
p_item_type in varchar2,
p_log_null_items in boolean)
is
l_app_session number;
l_app_id number;
l_log_null_item_yn varchar2(1);
l_item_type varchar2(30) := upper(p_item_type);
l_item_type_page_id number;
begin
$if $$no_op $then
null;
$else
$if $$apex $then
l_app_session := v('APP_SESSION');
l_app_id := v('APP_ID');
l_log_null_item_yn := 'N';
if p_log_null_items then
l_log_null_item_yn := 'Y';
end if;
if logger.is_number(l_item_type) then
l_item_type_page_id := to_number(l_item_type);
end if;
insert into logger_logs_apex_items(log_id,app_session,item_name,item_value)
select p_log_id, l_app_session, item_name, item_value
from (
-- Application items
select 1 app_page_seq, 0 page_id, item_name, v(item_name) item_value
from apex_application_items
where 1=1
and application_id = l_app_id
and l_item_type in (logger.g_apex_item_type_all, logger.g_apex_item_type_app)
union all
-- Application page items
select 2 app_page_seq, page_id, item_name, v(item_name) item_value
from apex_application_page_items
where 1=1
and application_id = l_app_id
and (
1=2
or l_item_type in (logger.g_apex_item_type_all, logger.g_apex_item_type_page)
or (l_item_type_page_id is not null and l_item_type_page_id = page_id)
)
)
where 1=1
and (l_log_null_item_yn = 'Y' or item_value is not null)
order by app_page_seq, page_id, item_name;
$end -- $if $$apex $then
null; -- Keep this in place incase APEX is not compiled
$end -- $$no_op
end snapshot_apex_items;
/**
* @private
* Wrapper to determine if the level is a valid level
*
*
* @example
*
* declare
* l_valid boolean;
* begin
* -- Note: this won't actually work since it's private function
* l_valid := logger.is_valid_level(p_level => logger.g_error_name);
* end;
* /
*
* @issue #184 Initial ticket.
*
* @author Martin D'Souza
* @created 30-May-2018
* @param p_level
* @return True of level is valid
*/
function is_valid_level(
p_level in varchar2)
return boolean
as
begin
return p_level in (g_off_name, g_permanent_name, g_error_name, g_warning_name, g_information_name, g_debug_name, g_timing_name, g_sys_context_name, g_apex_name);
end is_valid_level;
/**
* @private
* Returns all level names as a string
*
*
* @example
*
*
* @issue #184 Initial ticket.
*
* @author Martin D'Souza
* @created 30-May-2018
* @param p_delimiter delimeter for values
* @return list of levels delimited by p_delimiter
*/
function get_all_level_names(
p_delimiter in varchar2 default ', ')
return varchar2
as
begin
return
g_off_name || p_delimiter ||
g_permanent_name || p_delimiter ||
g_error_name || p_delimiter ||
g_warning_name || p_delimiter ||
g_information_name || p_delimiter ||
g_debug_name || p_delimiter ||
g_timing_name || p_delimiter ||
g_sys_context_name || p_delimiter ||
g_apex_name;
end get_all_level_names;
-- **** PUBLIC BUT NOT DOCUMENTED ****
/*!
* Sets all the contexts to null
*
* Notes:
* - Though this is public it is not a documented procedure. Only used with logger_configure.
*
* @issue#46 Plugin support
* @issue#110 Clear all contexts (including ones with client identifier)
*
* @author Tyler Muth
* @created ???
*/
procedure null_global_contexts
is
pragma autonomous_transaction;
begin
$if $$no_op or $$rac_lt_11_2 or not $$logger_context $then
null;
$else
dbms_session.clear_all_context(namespace => g_context_name);
$end
commit;
end null_global_contexts;
-- TODO mdsouza: see about dropping this as it's not being used
/*!
* Displays commonly used dbms_output SQL*Plus settings
*
* @author Tyler Muth
* @created ???
*/
procedure sqlplus_format
is
begin
execute immediate 'begin dbms_output.enable(1000000); end;';
dbms_output.put_line('set linesize 200');
dbms_output.put_line('set pagesize 100');
dbms_output.put_line('column id format 999999');
dbms_output.put_line('column text format a75');
dbms_output.put_line('column call_stack format a100');
dbms_output.put_line('column extra format a100');
end sqlplus_format;
-- TODO mdsouza: make private?
/*!
* Returns the rec_logger_logs for given logger_level
* Used for plugin.
* Not meant to be called by general public, and thus not documented
*
* Notes:
* - -- FUTURE mdsouza: Add tests for this (#86)
*
* Related Tickets:
* - #46
*
* @author Martin D'Souza
* @created 11-Mar-2015
* @param p_logger_level Logger level of plugin wanted to return
* @return Logger rec based on plugin type
*/
function get_plugin_rec(
p_logger_level in logger_logs.logger_level%type)
return logger.rec_logger_log
as
begin
if p_logger_level = logger.g_error then
return g_plug_logger_log_error;
end if;
end get_plugin_rec;