Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ rvm:
- 2.1.0
- jruby
- jruby-head
env: CI="travis"
env:
- CI="travis"
- CI="travis" MOPED_LOG_FORMAT="shell"
services:
- mongodb
matrix:
Expand Down
1 change: 1 addition & 0 deletions lib/moped.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "optionable"
require "moped/errors"
require "moped/indexes"
require "moped/log_format"
require "moped/loggable"
require "moped/uri"
require "moped/protocol"
Expand Down
157 changes: 157 additions & 0 deletions lib/moped/log_format.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
require "moped/log_format/utils/color_string"

module Moped
module LogFormat
# Get colorize flag
#
# @example Get colorize flag.
# DefaultFormat.colorize
#
# @return [ true | false ] Status of colorization.
#
# @since 2.0.0
def colorize
@@colorize
end

# Set colorize flag
#
# @example Disable colorization.
# DefaultFormat.colorize = false
#
# @param [ true | false ] flag Flag to set
#
# @since 2.0.0
def colorize=(flag)
@@colorize = !!flag
end

# Format the provided operations log entry.
#
# @example Log the operations.
# Default.log("MOPED", {}, 30)
#
# @param [ String ] prefix The prefix for all operations in the log.
# @param [ Array ] ops The operations.
# @param [ String ] runtime The runtime in formatted ms.
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def log(prefix, payload, runtime = nil)
raise NotImplemented
end

# Format the command event
#
# @example Format command.
# Default.command(event)
#
# @param [ Moped::Protocol::Command ] event Command event to format
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def command(event)
raise NotImplemented
end

# Format the insert event
#
# @example Format insert.
# Default.insert(event)
#
# @param [ Moped::Protocol::Insert ] event Insert event to format
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def insert(event)
raise NotImplemented
end

# Format the query event
#
# @example Format query.
# Default.query(event)
#
# @param [ Moped::Protocol::Query ] event Query event to format
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def query(event)
raise NotImplemented
end

# Format the query event
#
# @example Format query.
# Default.query(event)
#
# @param [ Moped::Protocol::Query ] event Query event to format
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def update(event)
raise NotImplemented
end

# Format the delete event
#
# @example Format delete.
# Default.delete(event)
#
# @param [ Moped::Protocol::Delete ] event Delete event to format
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def delete(event)
raise NotImplemented
end

# Format the cursor.next() event
#
# @example Format get_more.
# Default.get_more(event)
#
# @param [ Moped::Protocol::GetMore ] event GetMore event to format
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def get_more(event)
raise NotImplemented
end

# Format the cursor.close() event
#
# @example Format kill_cursors.
# Default.kill_cursors(event)
#
# @param [ Moped::Protocol::KillCursors ] event KillCursors event to format
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def kill_cursors(event)
raise NotImplemented
end

# Format the cursor.close() event
#
# @example Format kill_cursors.
# Default.kill_cursors(event)
#
# @param [ Moped::Protocol::KillCursors ] event KillCursors event to format
#
# @return [ ColorString ] ColorString
#
# @since 2.0.0
def color(string)
ColorString.new(string, self.colorize)
end
end
end
159 changes: 159 additions & 0 deletions lib/moped/log_format/default_format.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
require "moped/log_format"

module Moped
module LogFormat
class DefaultFormat
extend LogFormat

self.colorize = true

# Format the provided operations log entry.
#
# @example Log the operations.
# Default.log("MOPED", {}, 30)
#
# @param [ String ] prefix The prefix for all operations in the log.
# @param [ Array ] ops The operations.
# @param [ String ] runtime The runtime in formatted ms.
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def self.log(prefix, payload, runtime = nil)
[
color(prefix).magenta.bold,
payload,
color("runtime: #{runtime}").blue
].join(' ')
end

# Format the command event
#
# @example Format command.
# Default.command(event)
#
# @param [ Moped::Protocol::Command ] event Command event to format
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def self.command(event)
type = color("COMMAND").bold
"%-12s database=%s command=%s" % [type, event.database, event.selector.inspect]
end

# Format the insert event
#
# @example Format insert.
# Default.insert(event)
#
# @param [ Moped::Protocol::Insert ] event Insert event to format
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def self.insert(event)
type = color("INSERT").bold

"%-12s database=%s collection=%s documents=%s flags=%s" % [type, event.database,
event.collection, event.documents.inspect,
event.flags.inspect]
end

# Format the query event
#
# @example Format query.
# Default.query(event)
#
# @param [ Moped::Protocol::Query ] event Query event to format
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def self.query(event)
type = color("QUERY").bold
fields = []
fields << ["%-12s", type]
fields << ["database=%s", event.database]
fields << ["collection=%s", event.collection]
fields << ["selector=%s", event.selector.inspect]
fields << ["flags=%s", event.flags.inspect]
fields << ["limit=%s", event.limit.inspect]
fields << ["skip=%s", event.skip.inspect]
fields << ["batch_size=%s", event.batch_size.inspect]
fields << ["fields=%s", event.fields.inspect]
f, v = fields.transpose
f.join(" ") % v
end

# Format the query event
#
# @example Format query.
# Default.query(event)
#
# @param [ Moped::Protocol::Query ] event Query event to format
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def self.update(event)
type = color("UPDATE").bold

"%-12s database=%s collection=%s selector=%s update=%s flags=%s" % [type, event.database,
event.collection,
event.selector.inspect,
event.update.inspect,
event.flags.inspect]
end

# Format the delete event
#
# @example Format delete.
# Default.delete(event)
#
# @param [ Moped::Protocol::Delete ] event Delete event to format
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def self.delete(event)
type = color("DELETE").bold

"%-12s database=%s collection=%s selector=%s flags=%s" % [type, event.database, event.collection,
event.selector.inspect, event.flags.inspect]
end

# Format the cursor.next() event
#
# @example Format get_more.
# Default.get_more(event)
#
# @param [ Moped::Protocol::GetMore ] event GetMore event to format
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def self.get_more(event)
type = color("GET_MORE").bold
"%-12s database=%s collection=%s limit=%s cursor_id=%s" % [type, event.database,
event.collection, event.limit,
event.cursor_id]
end

# Format the cursor.close() event
#
# @example Format kill_cursors.
# Default.kill_cursors(event)
#
# @param [ Moped::Protocol::KillCursors ] event KillCursors event to format
#
# @return [ String ] Formatted string
#
# @since 2.0.0
def self.kill_cursors(event)
type = color("KILL_CURSORS").bold
"%-12s cursor_ids=%s" % [type, event.cursor_ids.inspect]
end
end
end
end
Loading