Class: Systemdy::Journal

Inherits:
Object
  • Object
show all
Extended by:
SingleForwardable
Defined in:
lib/systemdy/journal.rb

Overview

Allows to filter journalctl logs

Constant Summary collapse

LIST_OF_OPTIONS_THAT_NOT_ACCEPT_ARGUMENTS =
Note:

The meaning of this constant is that in linux the command “‘ journalctl -k “` not accept arguments

list of options for execute journalctl command that not accept arguments

{ kernel: '-k' }
LIST_OF_OPTIONS_THAT_ACCEPT_AN_ARGUMENT =
Note:

The meaning of this constant is that in linux the command “‘ journalctl -b “` can accept arguments, if executed without argument return the last boot logs

list of options for execute journalctl command that accept arguments

{ boot: '-b' }
LIST_OF_OPTIONS_THAT_REQUIRE_AN_ARGUMENT =
Note:

The meaning of this constant is that in linux the command like “‘ journalctl -u “` require arguments, if executed without argument return an error

list of options for execute journalctl command that require arguments

{ unit: '-u', group_id: '_GID', user_id: '_UID' }

Class Method Summary collapse

Class Method Details

.display_boot_logsArray

Note:

This method is generated with use of metaprogramming techniques

display the boot logs

Examples:

display the last 10 lines of kernel logs since today to the time execution method

Systemdy::Journal.display_boot_logs #=> [...]

display the last 50 log lines of the second boot ranging from a month ago to 10:00 of the current day

Systemdy::Journal.display_boot_logs(argument: 2, since: '1 month ago', to: '10:00', lines: 50) #=> [...]

display the last 20 log of the third boot lines ranging from a week ago to yesterday

Systemdy::Journal.display_boot_logs(argument: 3, since: '1 week ago', to: 'yesterday', lines: 20) #=> [...]

You can also filter the logs by specific dates in the format ‘YYYY-MM-DD’

Systemdy::Journal.display_boot_logs(since: '2022-08-27', lines: 200) #=> [...]

Parameters:

  • argument (Integer)

    the boot number

  • since (String)

    the log’s initial period - default: today

  • to (String)

    the log’s end period - default: timenow

  • lines (Integer)

    the log’s number of lines - default: 10

Returns:

  • (Array)

    a list of boot logs



69
70
71
72
73
74
75
76
# File 'lib/systemdy/journal.rb', line 69

LIST_OF_OPTIONS_THAT_ACCEPT_AN_ARGUMENT.each do |from, option|
    define_singleton_method "display_#{from}_logs" do |argument: '', since: 'today', to: Time.now.strftime('%H:%M'), lines: 10|
        # logs from system call
        logs = `#{JOURNALCTL_COMMAND} #{option} #{argument} -S '#{since}' -U '#{to}' -n #{lines} | tail -n #{lines} 2>&1`
        # logs from system call converted into array
        return_an_array_from_system_command(logs) # class method contained in Systemdy/utility/formatter.rb
    end
end

.display_group_id_logsArray

TODO:

This method require the GUID as argument. For more information check out the examples below

Note:

This method is generated with use of metaprogramming techniques

display the group_id logs (GUID)

Examples:

display the last 50 log lines of the GUID 1000 ranging from a month ago to 10:00 of the current day

Systemdy::Journal.display_group_id_logs(argument: 1000, since: '1 month ago', to: '10:00', lines: 50) #=> [...]

display the last 20 log lines of the GUID 1000 ranging from a week ago to yesterday

Systemdy::Journal.display_group_id_logs(argument: 1000, since: '1 week ago', to: 'yesterday', lines: 20) #=> [...]

You can also filter the logs by specific dates in the format ‘YYYY-MM-DD’

Systemdy::Journal.display_group_id_logs(argument: 1000, since: '2022-08-27', lines: 200) #=> [...]

Parameters:

  • argument (Integer)

    the group id

  • since (String)

    the log’s initial period - default: today

  • to (String)

    the log’s end period - default: timenow

  • lines (Integer)

    the log’s number of lines - default: 10

Returns:

  • (Array)

    a list of group id logs



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/systemdy/journal.rb', line 128

LIST_OF_OPTIONS_THAT_REQUIRE_AN_ARGUMENT.each do |from, option|
    define_singleton_method "display_#{from}_logs" do |argument: '', since: 'today', to: Time.now.strftime('%H:%M'), lines: 10|
        # return an error message if the required argument is not provided
        # render_message class method contained in Systemdy/utility/message_displayer.rb
        return render_message("display_#{from}_logs require an argument!") if argument.to_s.empty?
        # combination of option and argument based on typology
        # '-u postgresql' or '_GUID=1000' or '_UID=1000'
        option_with_argument = merge_option_with_argument_based_on_option_typology(from, option, argument)
        # logs from system call
        logs                 = `#{JOURNALCTL_COMMAND} #{option_with_argument} -S '#{since}' -U '#{to}' | tail -n #{lines} 2>&1`
        # logs from system call converted into array
        return_an_array_from_system_command(logs) # class method contained in Systemdy/utility/formatter.rb
    end
end

.display_kernel_logsArray

Note:

This method is generated with use of metaprogramming techniques

display the kernel logs

Examples:

display the last 10 lines of kernel logs since today to the time execution method

Systemdy::Journal.display_kernel_logs #=> [...]

display the last the last 20 log lines ranging from a week ago to yesterday

Systemdy::Journal.display_kernel_logs(since: '1 week ago', to: 'yesterday', lines: 20) #=> [...]

You can also filter the logs by specific dates in the format ‘YYYY-MM-DD’

Systemdy::Journal.display_kernel_logs(since: '2022-08-27', lines: 200) #=> [...]

Parameters:

  • since (String)

    the log’s initial period - default: today

  • to (String)

    the log’s end period - default: timenow

  • lines (Integer)

    the log’s number of lines - default: 10

Returns:

  • (Array)

    a list of kernel logs



41
42
43
44
45
46
47
48
# File 'lib/systemdy/journal.rb', line 41

LIST_OF_OPTIONS_THAT_NOT_ACCEPT_ARGUMENTS.each do |from, option|
    define_singleton_method "display_#{from}_logs" do |since: 'today', to: Time.now.strftime('%H:%M'), lines: 10|
        # logs from system call
        logs = `#{JOURNALCTL_COMMAND} #{option} -S '#{since}' -U '#{to}' -n #{lines} | tail -n #{lines} 2>&1`
        # logs from system call converted into array
        return_an_array_from_system_command(logs) # class method contained in Systemdy/utility/formatter.rb
    end
end

.display_unit_logsArray

TODO:

This method require the unit name as argument. For more information check out the examples below

Note:

This method is generated with use of metaprogramming techniques

display the unit logs

Examples:

display the last 50 log lines of the potsgresql service ranging from a month ago to 10:00 of the current day

Systemdy::Journal.display_unit_logs(argument: 'postgresql', since: '1 month ago', to: '10:00', lines: 50) #=> [...]

display the last 20 log lines of the potsgresql service ranging from a week ago to yesterday

Systemdy::Journal.display_unit_logs(argument: 'postgresql', since: '1 week ago', to: 'yesterday', lines: 20) #=> [...]

You can also filter the logs by specific dates in the format ‘YYYY-MM-DD’

Systemdy::Journal.display_unit_logs(argument: 'postgresql', since: '2022-08-27', lines: 200) #=> [...]

Parameters:

  • argument (string)

    the unit name

  • since (String)

    the log’s initial period - default: today

  • to (String)

    the log’s end period - default: timenow

  • lines (Integer)

    the log’s number of lines - default: 10

Returns:

  • (Array)

    a list of unit logs



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/systemdy/journal.rb', line 128

LIST_OF_OPTIONS_THAT_REQUIRE_AN_ARGUMENT.each do |from, option|
    define_singleton_method "display_#{from}_logs" do |argument: '', since: 'today', to: Time.now.strftime('%H:%M'), lines: 10|
        # return an error message if the required argument is not provided
        # render_message class method contained in Systemdy/utility/message_displayer.rb
        return render_message("display_#{from}_logs require an argument!") if argument.to_s.empty?
        # combination of option and argument based on typology
        # '-u postgresql' or '_GUID=1000' or '_UID=1000'
        option_with_argument = merge_option_with_argument_based_on_option_typology(from, option, argument)
        # logs from system call
        logs                 = `#{JOURNALCTL_COMMAND} #{option_with_argument} -S '#{since}' -U '#{to}' | tail -n #{lines} 2>&1`
        # logs from system call converted into array
        return_an_array_from_system_command(logs) # class method contained in Systemdy/utility/formatter.rb
    end
end

.display_user_id_logsArray

TODO:

This method require the UID as argument. For more information check out the examples below

Note:

This method is generated with use of metaprogramming techniques

display the user_id logs (UID)

Examples:

display the last 50 log lines of the UID 1000 ranging from a month ago to 10:00 of the current day

Systemdy::Journal.display_user_id_logs(argument: 1000, since: '1 month ago', to: '10:00', lines: 50) #=> [...]

display the last 20 log lines of the UID 1000 ranging from a week ago to yesterday

Systemdy::Journal.display_user_id_logs(argument: 1000, since: '1 week ago', to: 'yesterday', lines: 20) #=> [...]

You can also filter the logs by specific dates in the format ‘YYYY-MM-DD’

Systemdy::Journal.display_user_id_logs(argument: 1000, since: '2022-08-27', lines: 200) #=> [...]

Parameters:

  • argument (Integer)

    the user id

  • since (String)

    the log’s initial period - default: today

  • to (String)

    the log’s end period - default: timenow

  • lines (Integer)

    the log’s number of lines - default: 10

Returns:

  • (Array)

    a list of user id logs



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/systemdy/journal.rb', line 128

LIST_OF_OPTIONS_THAT_REQUIRE_AN_ARGUMENT.each do |from, option|
    define_singleton_method "display_#{from}_logs" do |argument: '', since: 'today', to: Time.now.strftime('%H:%M'), lines: 10|
        # return an error message if the required argument is not provided
        # render_message class method contained in Systemdy/utility/message_displayer.rb
        return render_message("display_#{from}_logs require an argument!") if argument.to_s.empty?
        # combination of option and argument based on typology
        # '-u postgresql' or '_GUID=1000' or '_UID=1000'
        option_with_argument = merge_option_with_argument_based_on_option_typology(from, option, argument)
        # logs from system call
        logs                 = `#{JOURNALCTL_COMMAND} #{option_with_argument} -S '#{since}' -U '#{to}' | tail -n #{lines} 2>&1`
        # logs from system call converted into array
        return_an_array_from_system_command(logs) # class method contained in Systemdy/utility/formatter.rb
    end
end