lib/timber/contexts/user.rb in timberio-1.0.0.beta1 vs lib/timber/contexts/user.rb in timberio-1.0.0
- old
+ new
@@ -1,39 +1,36 @@
module Timber
module Contexts
+ # The user context tracks the currently authenticated user.
+ #
+ # You will want to add this context at the time log the user in, typically
+ # during the authentication flow.
+ #
+ # Note: Timber will attempt to automatically add this if you add a #current_user
+ # method to your controllers. Most authentication solutions do this for you automatically.
+ #
+ # Example:
+ #
+ # user_context = Timber::Contexts::User.new(id: "abc1234", name: "Ben Johnson")
+ # Timber::CurrentContext.with(user_context) do
+ # # Logging will automatically include this context
+ # logger.info("This is a log message")
+ # end
+ #
class User < Context
- ROOT_KEY = :user.freeze
- VERSION = 1.freeze
+ attr_reader :id, :name
- attr_reader :user
-
- def email
- return @email if defined?(@email)
- @email = user.respond_to?(:email) ? user.email : nil
+ def initialize(attributes)
+ @id = attributes[:id]
+ @name = attributes[:name]
end
- def id
- return @id if defined?(@id)
- @id = user.respond_to?(:id) ? user.id : nil
+ def keyspace
+ :user
end
- def name
- return @name if defined?(@name)
- @name = user.respond_to?(:name) ? user.name : nil
+ def as_json(_options = {})
+ {id: id, name: name}
end
-
- def valid?
- !user.nil?
- end
-
- private
- def json_payload
- @json_payload ||= Macros::DeepMerger.merge({
- # order is relevant for logfmt styling
- :id => id,
- :name => name,
- :email => email
- }, super).freeze
- end
end
end
-end
+end
\ No newline at end of file