artifact_logger 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -4,10 +4,16 @@ This is an engine which is designed to be a drop-in logging agent for use in a R
4
4
 
5
5
  It's designed to be pretty simple and easy to use.
6
6
 
7
+ Installation:
8
+ # Gemfile
9
+ gem "artifact_logger"
10
+
11
+ # Run this
12
+ rake artifact_logger_engine:install:migrations
13
+
7
14
  Usage:
8
15
 
9
- # app/models/artifact.rb
10
-
16
+ # app/models/artifact.rb
11
17
  class Artifact < ActiveRecord::Base
12
18
  #levels are optional
13
19
  enable_artifact_logger :levels => [:info, :warning, :error, :custom]
@@ -21,11 +27,11 @@ Usage:
21
27
 
22
28
  a = Artifact.new
23
29
 
24
- # To create an error or info message in the log
30
+ # To create an error or info message in the log
25
31
  m = a.log "error", "This is an error message!"
26
32
  n = a.log.info "This is an error message!"
27
33
 
28
- #Both of these will display the same information
34
+ # Both of these will display the same information
29
35
  a.log #> [m, n]
30
36
  a.log.error #> [m, n]
31
37
 
@@ -33,7 +39,7 @@ Usage:
33
39
  m.artifact_type #> Artifact
34
40
 
35
41
 
36
- #This is also usable on the model itself
42
+ # This is also usable on the model itself
37
43
  info = Article.log "info", "Article did something"
38
44
  warn = Article.log.warning "Article did something else"
39
45
 
data/app/models/log.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Log
2
+ def self.table_name_prefix
3
+ 'log_'
4
+ end
5
+ end
@@ -0,0 +1,47 @@
1
+ class Log::Message < ActiveRecord::Base
2
+ belongs_to :artifact, :polymorphic => true
3
+
4
+ validates :text, :presence => true
5
+ validates :level, :presence => true, :inclusion => { :in => lambda { |record| record.valid_levels } }
6
+
7
+ scope :log_level, lambda { |level| where(:level => level)}
8
+
9
+ def self.valid_levels
10
+ [:info, :warning, :error]
11
+ end
12
+
13
+ def valid_levels
14
+ if artifact_type
15
+ artifact_type.valid_log_levels || self.class.valid_levels
16
+ else
17
+ self.class.valid_levels
18
+ end
19
+ end
20
+
21
+ #Store the old artifact and artifact= functions for later usage
22
+ alias_method :_get_artifact, :artifact
23
+ alias_method :_set_artifact, :artifact=
24
+
25
+ #Returns either the model, or the object
26
+ def artifact
27
+ _get_artifact.nil? ? artifact_type : _get_artifact
28
+ end
29
+
30
+ #Assignment overriden to allow the model to be used as well
31
+ def artifact= val
32
+ if val.is_a? Class
33
+ self[:artifact_type] = val.model_name
34
+ else
35
+ _set_artifact val
36
+ end
37
+ end
38
+
39
+ #Finds the class of object which we're dealing with
40
+ def artifact_type
41
+ self[:artifact_type].nil? ? nil : self[:artifact_type].constantize
42
+ end
43
+
44
+ def level= level
45
+ super level.to_sym
46
+ end
47
+ end
@@ -0,0 +1,21 @@
1
+ class CreateArtifactLogger < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :log_messages do |t|
4
+ t.string :level, :null => false
5
+ # t.string :type
6
+ t.text :text, :null => false
7
+ t.string :artifact_type
8
+ t.integer :artifact_id
9
+
10
+ t.timestamps
11
+ end
12
+
13
+ add_index :log_messages, :artifact_type
14
+ add_index :log_messages, :artifact_id
15
+ add_index :log_messages, :level
16
+ end
17
+
18
+ def self.down
19
+ drop_table :artifacts
20
+ end
21
+ end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: artifact_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - ajrkerr
8
+ - Adam T Kerr
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
@@ -24,6 +24,9 @@ files:
24
24
  - lib/artifact_logger/engine.rb
25
25
  - lib/tasks/artifact_logger_tasks.rake
26
26
  - lib/artifact_logger.rb
27
+ - app/models/log.rb
28
+ - app/models/log/message.rb
29
+ - db/migrate/20110706154303_create_artifact_logger.rb
27
30
  - MIT-LICENSE
28
31
  - Rakefile
29
32
  - README.rdoc