Skip to content

Commit

Permalink
Add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Nov 14, 2019
1 parent de06aed commit fe85898
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions spec/support/db.cr
@@ -1,6 +1,7 @@
require "./factory"

ShardsDB.database_url = ENV["TEST_DATABASE_URL"]
ShardsDB.logger = Logger.new(nil)

def transaction
ShardsDB.transaction do |db, transaction|
Expand Down
20 changes: 20 additions & 0 deletions src/db.cr
Expand Up @@ -409,7 +409,27 @@ class ShardsDB
end

# LOGGING
class_property logger : Logger { Logger.new(STDOUT) }

def log_activity(event : String, repo_id : Int64? = nil, shard_id : Int64? = nil, metadata = nil)
log_message = String.build do |io|
io << event
if repo_id
repo_ref = get_repo(repo_id).ref rescue nil
repo_ref ||= repo_id
io << " repo=" << repo_ref
end
if shard_id
io << " shard=" << shard_id
end
if metadata
metadata.each do |key, value|
io << " " << key << "="
value.inspect(io)
end
end
end
self.class.logger.info(log_message)
connection.exec <<-SQL, event, repo_id, shard_id, metadata.to_json
INSERT INTO activity_log
(
Expand Down

0 comments on commit fe85898

Please sign in to comment.