|  line  | 
 stmt  | 
 bran  | 
 cond  | 
 sub  | 
 pod  | 
 time  | 
 code  | 
| 
1
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 package Dancer::Plugin::Log::DB;  | 
| 
2
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
3
 | 
8
 | 
 
 | 
 
 | 
  
8
  
 | 
 
 | 
2533588
 | 
 use strict;  | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
21
 | 
    | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
425
 | 
    | 
| 
4
 | 
8
 | 
 
 | 
 
 | 
  
8
  
 | 
 
 | 
43
 | 
 use warnings;  | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
16
 | 
    | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
202
 | 
    | 
| 
5
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
6
 | 
8
 | 
 
 | 
 
 | 
  
8
  
 | 
 
 | 
42
 | 
 use Carp;  | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
22
 | 
    | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
576
 | 
    | 
| 
7
 | 
8
 | 
 
 | 
 
 | 
  
8
  
 | 
 
 | 
46
 | 
 use List::Util 'first';  | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
13
 | 
    | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
580
 | 
    | 
| 
8
 | 
8
 | 
 
 | 
 
 | 
  
8
  
 | 
 
 | 
1020
 | 
 use Time::Piece;  | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
17164
 | 
    | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
81
 | 
    | 
| 
9
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
10
 | 
8
 | 
 
 | 
 
 | 
  
8
  
 | 
 
 | 
7354
 | 
 use Dancer::Plugin;  | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
101322
 | 
    | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
686
 | 
    | 
| 
11
 | 
8
 | 
 
 | 
 
 | 
  
8
  
 | 
 
 | 
64
 | 
 use Dancer::Config;  | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
16
 | 
    | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
254
 | 
    | 
| 
12
 | 
8
 | 
 
 | 
 
 | 
  
8
  
 | 
 
 | 
7446
 | 
 use Dancer::Plugin::Database;  | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
513993
 | 
    | 
| 
 
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
5821
 | 
    | 
| 
13
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
14
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 our $VERSION = '0.04';  | 
| 
15
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
16
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 our $settings = undef;  | 
| 
17
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 our $dbh;  | 
| 
18
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
19
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 my %db_defaults = (  | 
| 
20
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	driver => 'SQLite',  | 
| 
21
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	database => 'test',  | 
| 
22
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	username => 'myusername',  | 
| 
23
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	password => 'password',  | 
| 
24
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	host => 'localhost',  | 
| 
25
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	port => '3306',  | 
| 
26
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 );  | 
| 
27
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
28
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 sub _setup_connection {  | 
| 
29
 | 
14
 | 
  
100
  
 | 
 
 | 
  
14
  
 | 
 
 | 
105
 | 
 	$settings = plugin_setting() if !$settings;  | 
| 
30
 | 
14
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
144
 | 
 	return unless $settings;  | 
| 
31
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	  | 
| 
32
 | 
14
 | 
 
 | 
 
 | 
 
 | 
 
 | 
29
 | 
 	my %connection_params;  | 
| 
33
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	  | 
| 
34
 | 
14
 | 
 
 | 
 
 | 
 
 | 
 
 | 
43
 | 
 	for (qw/driver database username password host port/) {  | 
| 
35
 | 
84
 | 
 
 | 
  
 66
  
 | 
 
 | 
 
 | 
484
 | 
 		$connection_params{$_} = $settings->{database}{$_} || $db_defaults{$_};  | 
| 
36
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	}  | 
| 
37
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	  | 
| 
38
 | 
14
 | 
 
 | 
 
 | 
 
 | 
 
 | 
92
 | 
 	$dbh = database(\%connection_params);  | 
| 
39
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 }  | 
| 
40
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
41
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 register log_db_dbh => sub {  | 
| 
42
 | 
31
 | 
  
100
  
 | 
 
 | 
  
31
  
 | 
 
 | 
130793
 | 
 	$dbh = _setup_connection() unless $dbh;  | 
| 
43
 | 
31
 | 
 
 | 
 
 | 
 
 | 
 
 | 
62375
 | 
 	return $dbh;  | 
| 
44
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 };  | 
| 
45
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
46
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 register log_db => sub {  | 
| 
47
 | 
9
 | 
 
 | 
 
 | 
  
9
  
 | 
 
 | 
1752488
 | 
 	my $params = shift;  | 
| 
48
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
49
 | 
9
 | 
 
 | 
 
 | 
 
 | 
 
 | 
41
 | 
 	_setup_connection();  | 
| 
50
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
51
 | 
9
 | 
 
 | 
  
100
  
 | 
 
 | 
 
 | 
14590
 | 
 	my $message_field_name = $settings->{log}->{message_field_name} || 'message';  | 
| 
52
 | 
9
 | 
 
 | 
  
100
  
 | 
 
 | 
 
 | 
68
 | 
 	my $timestamp_field_name = $settings->{log}->{timestamp_field_name} || 'timestamp';  | 
| 
53
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
54
 | 
9
 | 
 
 | 
  
100
  
 | 
 
 | 
 
 | 
60
 | 
 	my $logs_table_name = $settings->{log}->{logs_table_name} || 'logs';  | 
| 
55
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
56
 | 
9
 | 
 
 | 
 
 | 
 
 | 
 
 | 
27
 | 
 	my $additional_fields = $settings->{log}->{additional_fields};  | 
| 
57
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
58
 | 
9
 | 
 
 | 
 
 | 
 
 | 
 
 | 
18
 | 
 	my (@fields, @bind);  | 
| 
59
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
60
 | 
9
 | 
 
 | 
  
 50
  
 | 
 
 | 
 
 | 
39
 | 
 	my $message = $params->{message} || return;  | 
| 
61
 | 
9
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
85
 | 
 	my $timestamp = $params->{timestamp} ? localtime($params->{timestamp}) : localtime;  | 
| 
62
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	  | 
| 
63
 | 
9
 | 
 
 | 
 
 | 
 
 | 
 
 | 
1882
 | 
 	push @fields, $message_field_name;  | 
| 
64
 | 
9
 | 
 
 | 
 
 | 
 
 | 
 
 | 
20
 | 
 	push @fields, $timestamp_field_name;  | 
| 
65
 | 
9
 | 
 
 | 
 
 | 
 
 | 
 
 | 
21
 | 
 	push @bind, $message;  | 
| 
66
 | 
9
 | 
 
 | 
 
 | 
 
 | 
 
 | 
57
 | 
 	push @bind, sprintf("%s %s", $timestamp->ymd, $timestamp->hms);  | 
| 
67
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	  | 
| 
68
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	# Handle additional field values  | 
| 
69
 | 
9
 | 
 
 | 
 
 | 
 
 | 
 
 | 
811
 | 
 	delete $params->{message};  | 
| 
70
 | 
9
 | 
 
 | 
 
 | 
 
 | 
 
 | 
22
 | 
 	delete $params->{timestamp};  | 
| 
71
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	  | 
| 
72
 | 
9
 | 
  
100
  
 | 
 
 | 
 
 | 
 
 | 
50
 | 
 	if (scalar %$params) {  | 
| 
73
 | 
3
 | 
 
 | 
 
 | 
 
 | 
 
 | 
17
 | 
 		while (my ($key, $value) = each %$params) {  | 
| 
74
 | 
5
 | 
  
100
  
 | 
 
 | 
  
6
  
 | 
 
 | 
62
 | 
 			unless (first { $_ eq $key } @$additional_fields) {  | 
| 
 
 | 
6
 | 
 
 | 
 
 | 
 
 | 
 
 | 
42
 | 
    | 
| 
75
 | 
1
 | 
 
 | 
 
 | 
 
 | 
 
 | 
38
 | 
 				Dancer::Logger::error("Non-described field: $key. Add it to 'additional_fields'");  | 
| 
76
 | 
1
 | 
 
 | 
 
 | 
 
 | 
 
 | 
89
 | 
 				return;  | 
| 
77
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 			}  | 
| 
78
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
79
 | 
4
 | 
 
 | 
 
 | 
 
 | 
 
 | 
20
 | 
 			push @fields, $key;  | 
| 
80
 | 
4
 | 
 
 | 
 
 | 
 
 | 
 
 | 
18
 | 
 			push @bind, $value;  | 
| 
81
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 		}		  | 
| 
82
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	}  | 
| 
83
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 		  | 
| 
84
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
21
 | 
 	my $placeholders = join ",", map { "?" } @fields;  | 
| 
 
 | 
20
 | 
 
 | 
 
 | 
 
 | 
 
 | 
53
 | 
    | 
| 
85
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
191
 | 
 	my $query = "INSERT INTO ${logs_table_name} (" . join (",", @fields) . ") VALUES($placeholders)";  | 
| 
86
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
87
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
107
 | 
 	my $sth = $dbh->prepare($query);  | 
| 
88
 | 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
283593
 | 
 	$sth->execute(@bind);  | 
| 
89
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 };  | 
| 
90
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
91
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 register_plugin;  | 
| 
92
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
93
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 1; # End of Dancer::Plugin::Log::DB  | 
| 
94
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
95
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 __END__  |