# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'rubygems' require 'connection/RESTConnection' require 'util/util' require 'json/pure' require 'App42_Ruby_API/App42Response' require 'log/LogResponseBuilder' require 'log/Log' module App42 module Log # # Centralize logging for your App. This service allows different levels e.g. info, # debug, fatal, error etc. to log a message and query the messages based on # different parameters. # # You can fetch logs based on module, level, message, date range etc. # # @see Log # class LogService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "LogService->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "log" @version = "1.0" end # # Builds and Logs the message # # @param msg # - Message to be logged # @param module # - Module name for which the message is getting logged # @param level # - The level on which the message is getting logged # # @return Log object containing logged message # # @throws App42Exception # def build_and_send(msg,appModule,level) puts "Build and Send Called " puts "Base url #{@base_url}" response = nil logObj = nil logObj = Log.new util = Util.new util.throwExceptionIfNullOrBlank(msg, "Message"); util.throwExceptionIfNullOrBlank(appModule, "App Module"); util.throwExceptionIfNullOrBlank(appModule, "Level"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"log"=> { "message" => msg, "appModule" => appModule }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{level}" response = connection.post(signature, resource_url, query_params, body) log = LogResponseBuilder.new() logObj = log.buildResponse(response); rescue App42Exception =>e puts e raise e rescue Exception => e puts e raise App42Exception.new(e) end return logObj end # # Logs the info message # # @param msg # - Message to be logged # @param module # - Module name for which the message is getting logged # # @return Log object containing logged message # # @raise App42Exception # def info( msg, appModule) return build_and_send(msg, appModule, "info"); end # # Logs the debug message # # @param msg # - Message to be logged # @param module # - Module name for which the message is getting logged # # @return Log object containing logged message # # @raise App42Exception # def debug( msg, appModule) return build_and_send(msg, appModule, "debug"); end # # Logs the fatal message # # @param msg # - Message to be logged # @param module # - Module name for which the message is getting logged # # @return Log object containing logged message # # @raise App42Exception # def fatal( msg, appModule) return build_and_send(msg, appModule, "fatal"); end # # Logs the error message # # @param msg # - Message to be logged # @param module # - Module name for which the message is getting logged # # @return Log object containing logged message # # @raise App42Exception # def error( msg, appModule) return build_and_send(msg, appModule, "error"); end # # Fetch the log messages based on the Module # # @param moduleName # - Module name for which the messages has to be fetched # # @return Log object containing fetched messages # # @raise App42Exception # def fetch_logs_by_module(moduleName) puts "Fetch Logs By Module Called " puts "Base url #{@base_url}" response = nil logObj = nil logObj = Log.new util = Util.new util.throwExceptionIfNullOrBlank(moduleName, "ModuleName"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("moduleName", moduleName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/module/#{moduleName}" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" logObj = LogResponseBuilder.new().buildResponse(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return logObj end # # Fetch the log messages based on the Module by paging. # # @param moduleName # - Module name for which the messages has to be fetched # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return Log object containing fetched messages # # @raise App42Exception # def fetch_logs_by_module_by_paging(moduleName, max, offset) puts "Fetch Logs By Module By Paging Called " puts "Base url #{@base_url}" response = nil logObj = nil logObj = Log.new util = Util.new util.validateMax(max); util.throwExceptionIfNullOrBlank(moduleName, "ModuleName"); util.throwExceptionIfNullOrBlank(max, "Max"); util.throwExceptionIfNullOrBlank(offset, "Offset"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("moduleName", moduleName) params.store("max", "" + (max.to_i).to_s) params.store("offset", "" + (offset.to_i).to_s) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/paging/module/#{moduleName}/#{(max.to_i).to_s}/#{(offset.to_i).to_s}" response = connection.get(signature, resource_url, query_params) logObj = LogResponseBuilder.new().buildResponse(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end puts logObj return logObj end # # Fetch log messages based on the Module and Message Text # # @param moduleName # - Module name for which the messages has to be fetched # @param text # - The log message on which logs have to be searched # # @return Log object containing fetched messages # # @raise App42Exception # def fetch_logs_by_module_and_text(moduleName, text) puts "Fetch Logs By Module And Text Called " puts "Base url #{@base_url}" response = nil logObj = nil logObj = Log.new util = Util.new util.throwExceptionIfNullOrBlank(moduleName, "ModuleName"); util.throwExceptionIfNullOrBlank(text, "Text"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("moduleName", moduleName) params.store("text", text) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/module/#{moduleName}/text/#{text}" response = connection.get(signature, resource_url, query_params) logObj = LogResponseBuilder.new().buildResponse(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return logObj end # # Fetch log messages based on the Module and Message Text by paging. # # @param moduleName # - Module name for which the messages has to be fetched # @param text # - The log message on which logs have to be searched # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return Log object containing fetched messages # # @raise App42Exception # def fetch_logs_by_module_and_text_by_paging(moduleName, text, max, offset) puts "Fetch Logs By Module And Text By Paging Called " puts "Base url #{@base_url}" response = nil logObj = nil logObj = Log.new util = Util.new util.validateMax(max); util.throwExceptionIfNullOrBlank(moduleName, "ModuleName"); util.throwExceptionIfNullOrBlank(text, "Text"); util.throwExceptionIfNullOrBlank(max, "Max"); util.throwExceptionIfNullOrBlank(offset, "Offset"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("moduleName", moduleName) params.store("text", text) params.store("max", "" + (max.to_i).to_s) params.store("offset", "" + (offset.to_i).to_s) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/paging/module/#{moduleName}/text/#{text}/#{(max.to_i).to_s}/#{(offset.to_i).to_s}" response = connection.get(signature, resource_url, query_params) logObj = LogResponseBuilder.new().buildResponse(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return logObj end # # Fetch the log messages based on the Level # # @param level # - The level on which logs have to be searched # # @return Log object containing fetched messages # # @raise App42Exception # def fetch_logs_by_level(level) puts "Fetch Logs By Leve Called " puts "Base url #{@base_url}" response = nil logObj = nil logObj = Log.new util = Util.new util.throwExceptionIfNullOrBlank(level, "Level"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("type", level) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/type/#{level}" response = connection.get(signature, resource_url, query_params) logObj = LogResponseBuilder.new().buildResponse(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return logObj end # # Fetch the log messages based on the Level by paging. # # @param level # - The level on which logs have to be searched # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return Log object containing fetched messages # # @raise App42Exception # def fetch_logs_by_level_by_paging(level, max, offset) puts "Fetch Logs By Level By Paging Called " puts "Base url #{@base_url}" response = nil logObj = nil logObj = Log.new util = Util.new util.validateMax(max); util.throwExceptionIfNullOrBlank(level, "Level"); util.throwExceptionIfNullOrBlank(max, "Max"); util.throwExceptionIfNullOrBlank(offset, "Offset"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("type", level) params.store("max", "" + (max.to_i).to_s) params.store("offset", "" + (offset.to_i).to_s) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/paging/type/#{level}/#{(max.to_i).to_s}/#{(offset.to_i).to_s}" response = connection.get(signature, resource_url, query_params) logObj = LogResponseBuilder.new().buildResponse(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return logObj end # # Fetch log messages based on Info Level # # @return Log object containing fetched info messages # # @raise App42Exception # def fetch_logs_by_info() return fetch_logs_by_level("INFO"); end # # Fetch log messages based on Info Level by paging. # # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return Log object containing fetched info messages # # @raise App42Exception # def fetch_logs_by_info_by_paging(max, offset) return fetch_logs_by_level_by_paging("INFO", max, offset); end # # Fetch log messages based on Debug Level by paging. # # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return Log object containing fetched debug messages # # @raise App42Exception # def fetch_logs_by_debug_by_paging(max, offset) return fetch_logs_by_level_by_paging("DEBUG", max, offset); end # # Fetch log messages based on Error Level by paging. # # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return Log object containing fetched error messages # # @raise App42Exception # def fetch_logs_by_error_by_paging(max, offset) return fetch_logs_by_level_by_paging("ERROR", max, offset); end # # Fetch log messages based on Fatal Level by paging. # # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return Log object containing fetched Fatal messages # # @raise App42Exception # def fetch_logs_by_fatal_by_paging(max, offset) return fetch_logs_by_level_by_paging("FATAL", max, offset); end # # Fetch log messages based on Date range by paging. # # @param startDate # - Start date from which the log messages have to be fetched # @param endDate # - End date upto which the log messages have to be fetched # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return Log object containing fetched messages # # @raise App42Exception # def fetch_log_by_date_range_by_paging(startDate, endDate, max, offset) puts "Fetch Logs By Date Range By Paging Called " puts "Base url #{@base_url}" response = nil logObj = nil logObj = Log.new util = Util.new util.validateMax(max); util.throwExceptionIfNullOrBlank(max, "Max"); util.throwExceptionIfNullOrBlank(offset, "Offset"); util.throwExceptionIfNullOrBlank(startDate, "Start Date"); util.throwExceptionIfNullOrBlank(endDate, "End Date"); strStartdate = util.get_timestamp_utc_from_date(startDate) strEnddate = util.get_timestamp_utc_from_date(endDate) begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone strStartDate = util.get_timestamp_utc_from_date(startDate); strEndDate = util.get_timestamp_utc_from_date(endDate); params.store("startDate", strStartdate) params.store("endDate", strEnddate) params.store("max", "" + (max.to_i).to_s) params.store("offset", "" + (offset.to_i).to_s) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/paging/startDate/#{strStartdate}/endDate/#{strEnddate}/#{(max.to_i).to_s}/#{(offset.to_i).to_s}" response = connection.get(signature, resource_url, query_params) logObj = LogResponseBuilder.new().buildResponse(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return logObj end # # Fetch log messages based on Debug Level # # @return Log object containing fetched debug messages # # @raise App42Exception # def fetch_logs_by_debug() return fetch_logs_by_level("DEBUG"); end # # Fetch log messages based on Error Level # # @return Log object containing fetched error messages # # @raise App42Exception # def fetch_logs_by_error() return fetch_logs_by_level("ERROR"); end # # Fetch log messages based on Fatal Level # # @return Log object containing fetched Fatal messages # # @raise App42Exception # def fetch_logs_by_fatal() return fetch_logs_by_level("FATAL"); end # # Fetch log messages based on Date range # # @param startDate # - Start date from which the log messages have to be fetched # @param endDate # - End date upto which the log messages have to be fetched # # @return Log object containing fetched messages # # @raise App42Exception # def fetch_log_by_date_range(startDate, endDate) puts "Fetch Logs By Date Range Called " puts "Base url #{@base_url}" response = nil logObj = nil logObj = Log.new util = Util.new util.throwExceptionIfNullOrBlank(startDate, "Start Date"); util.throwExceptionIfNullOrBlank(endDate, "End Date"); strStartdate = util.get_timestamp_utc_from_date(startDate) strEnddate = util.get_timestamp_utc_from_date(endDate) begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone strStartDate = util.get_timestamp_utc_from_date(startDate); strEndDate = util.get_timestamp_utc_from_date(endDate); params.store("startDate", strStartdate) params.store("endDate", strEnddate) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/startDate/#{strStartdate}/endDate/#{strEnddate}" response = connection.get(signature, resource_url, query_params) logObj = LogResponseBuilder.new().buildResponse(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return logObj end # # Fetch the count of log messages based on the Module # # @param moduleName # - Module name for which the count of messages has to be fetched # # @return App42Response object containing count of fetched messages # # @raise App42Exception # def fetch_logs_count_by_module(moduleName) puts "Fetch Logs Count By Module Called " puts "Base url #{@base_url}" response = nil responseObj = App42Response.new util = Util.new util.throwExceptionIfNullOrBlank(moduleName, "ModuleName"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("moduleName", moduleName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/module/#{moduleName}/count" response = connection.get(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) responseObj = LogResponseBuilder.new() responseObj.getTotalRecords(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Fetch count of log messages based on the Module and Message Text # # @param moduleName # - Module name for which the count of messages has to be fetched # @param text # - The log message on which count of logs have to be searched # # @return App42Response object containing count of fetched messages # # @raise App42Exception # def fetch_logs_count_by_module_and_text(moduleName, text) puts "Fetch Logs Count By Module And Text Called " puts "Base url #{@base_url}" response = nil responseObj = App42Response.new util = Util.new util.throwExceptionIfNullOrBlank(moduleName, "ModuleName"); util.throwExceptionIfNullOrBlank(text, "Text"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("moduleName", moduleName) params.store("text", text); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/module/#{moduleName}/text/#{text}/count" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) responseObj = LogResponseBuilder.new() responseObj.getTotalRecords(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Fetch the count of log messages based on the Level # # @param level # - The level on which count of logs have to be searched # # @return App42Response object containing count of fetched messages # # @raise App42Exception # def fetch_logs_count_by_level(level) puts "Fetch Logs Count By Level Called " puts "Base url #{@base_url}" response = nil responseObj = App42Response.new util = Util.new util.throwExceptionIfNullOrBlank(level, "Level"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("type", level) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/type/#{level}/count" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) responseObj = LogResponseBuilder.new() responseObj.getTotalRecords(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Fetch count of log messages based on Info Level # # @return App42Response object containing count of fetched info messages # # @raise App42Exception # def fetch_logs_count_by_info() return fetch_logs_count_by_level("INFO"); end # # Fetch count of log messages based on Debug Level # # @return App42Response object containing count of fetched debug messages # # @raise App42Exception # def fetch_logs_count_by_debug() return fetch_logs_count_by_level("DEBUG"); end # # Fetch count of log messages based on Error Level # # @return App42Response object containing count of fetched error messages # # @raise App42Exception # def fetch_logs_count_by_error() return fetch_logs_count_by_level("ERROR"); end # # Fetch count of log messages based on Fatal Level # # @return App42Response object containing count of fetched Fatal messages # # @raise App42Exception # def fetch_logs_count_by_fatal() return fetch_logs_count_by_level("FATAL"); end # # Fetch count of log messages based on Date range # # @param startDate # - Start date from which the count of log messages have to be fetched # @param endDate # - End date upto which the count of log messages have to be fetched # # @return App42Response object containing count of fetched messages # # @raise App42Exception # def fetch_logs_count_by_date_range(startDate, endDate) puts "Fetch Logs Count By Date Range Called " puts "Base url #{@base_url}" response = nil responseObj = App42Response.new util = Util.new util.throwExceptionIfNullOrBlank(startDate, "Start Date"); util.throwExceptionIfNullOrBlank(endDate, "End Date"); strStartdate = util.get_timestamp_utc_from_date(startDate) strEnddate = util.get_timestamp_utc_from_date(endDate) begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone strStartDate = util.get_timestamp_utc_from_date(startDate); strEndDate = util.get_timestamp_utc_from_date(endDate); params.store("startDate", strStartdate) params.store("endDate", strEnddate) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/startDate/#{strStartdate}/endDate/#{strEnddate}/count" response = connection.get(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) responseObj = LogResponseBuilder.new() responseObj.getTotalRecords(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end end end end