Class: OnlyofficeMysqlHelper::MySQLLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/onlyoffice_mysql_helper/mysql_logger.rb

Overview

Log action in mysql

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mysql = MySQLHelper.new, table = nil, hash = {}) ⇒ MySQLLogger

Returns a new instance of MySQLLogger.



13
14
15
16
17
# File 'lib/onlyoffice_mysql_helper/mysql_logger.rb', line 13

def initialize(mysql = MySQLHelper.new, table = nil, hash = {})
  @mysql = mysql
  @table = table
  @hash = hash
end

Instance Attribute Details

#hashHash

Returns hash to add to DB.

Returns:

  • (Hash)

    hash to add to DB



11
12
13
# File 'lib/onlyoffice_mysql_helper/mysql_logger.rb', line 11

def hash
  @hash
end

#mysqlMySQLHelper

Returns instance of helper.

Returns:



7
8
9
# File 'lib/onlyoffice_mysql_helper/mysql_logger.rb', line 7

def mysql
  @mysql
end

#tableString

Returns table name.

Returns:

  • (String)

    table name



9
10
11
# File 'lib/onlyoffice_mysql_helper/mysql_logger.rb', line 9

def table
  @table
end

Instance Method Details

#create_log_table(table_name, column) ⇒ Void

Create table for logging purposes

Parameters:

  • table_name (String)

    name of table to create

Returns:

  • (Void)


22
23
24
25
26
27
28
# File 'lib/onlyoffice_mysql_helper/mysql_logger.rb', line 22

def create_log_table(table_name, column)
  table_command = 'id INT PRIMARY KEY AUTO_INCREMENT, '\
                  "#{column} VARCHAR(25) NOT NULL, "\
                  'time VARCHAR(255) NOT NULL, '\
                  'operation VARCHAR(255) NOT NULL'
  @mysql.create_table(table_name, table_command)
end

#log_actions(action, hash) ⇒ nil

Log ant actions in DB

Parameters:

  • action (String)

    name for log

  • hash (Hash)

    with additional options

Returns:

  • (nil)


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/onlyoffice_mysql_helper/mysql_logger.rb', line 34

def log_actions(action, hash)
  mysql_hash = hash.merge(@hash)
  mysql_hash[:time] = Time.now
  mysql_hash[:operation] = "Start: #{action}"
  @mysql.add_record(@table, mysql_hash)

  yield

  mysql_hash[:time] = Time.now
  mysql_hash[:operation] = "Finished: #{action}"
  @mysql.add_record(@table, mysql_hash)
end