log-pretty 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 613fbcdce0669a518c2fb589a310e6a73ac9c0fb873421775d0a7eeef297c3b9
4
- data.tar.gz: df60f6c0aa702875d08963ad6a90e51548e8d748f5efdd4eebdf7f9a4afc0f00
3
+ metadata.gz: a514b47d9728fc36838766ddf7dfb468b95fadeb34c973e7582416e0aa29b8a6
4
+ data.tar.gz: 4c9089d5dce89c36002566785fd2d8c67d188a1d4a1383b549dcb22f134a9b1e
5
5
  SHA512:
6
- metadata.gz: 30bccc61a218863ac2d1d11306025b29248783cd2347ee75e568aa741a740ee346a08bb5c85c7340d78fe58637991232ccd24908367a84bc5d85f2209e991e54
7
- data.tar.gz: ec97acb6918fcb4857370b654b536846eeae2016a80c64ba5d4c3f2260996f2dadd6f8be190ffdd7166087184eced635b253fbf9786dca5131bf1a5ff926458a
6
+ metadata.gz: dc1a186bb696547c179294975c6d792367f80d452f5a4b12dadf71db24c85f4774ec9fc7418f023e85e808fada76940aa1085f50fa7415215f2070916c37cee4
7
+ data.tar.gz: 8ca089c467d000e22de03443df35e2f5ecc7d1e898de41ca0edd7485b60e50b17370f08761f72a2cc11adc36fc9df0212f301e9072bad752103f7cb26e091906
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # LogPretty
2
2
 
3
- LogPretty adds functionaltiy to print to STDOUT using [ANSI escape](https://en.wikipedia.org/wiki/ANSI_escape_code) colors and formatting.
3
+ LogPretty adds functionaltiy to print to STDOUT or any logging device using [ANSI escape](https://en.wikipedia.org/wiki/ANSI_escape_code) colors and formatting.
4
4
 
5
5
  You can echo the following to any ANSI-compatible terminal to see blue output.
6
6
 
@@ -8,7 +8,7 @@ You can echo the following to any ANSI-compatible terminal to see blue output.
8
8
  $ echo "\e[34mHello world!\e[0m"
9
9
  ```
10
10
 
11
- The '34' is the code for blue and the '0' at the end resets all styles. You can find a helpful list of ANSI codes [here](https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797).
11
+ The '34' is the code for blue and the '0' at the end resets all styles. You can find a helpful list of ANSI codes [here](https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797).
12
12
 
13
13
  ## Installation
14
14
 
@@ -34,7 +34,7 @@ include LogPretty
34
34
 
35
35
  ## Usage
36
36
 
37
- The main method `#logp` (which stands for 'log pretty') logs pretty content (colors, formatting, etc.) to standard output. This can be useful for writing scrips and CLIs where you would like the user to see prettier content in their terminal.
37
+ The main method `#logp` (which stands for 'log pretty') logs pretty content (colors, formatting, etc.) to standard output. This can be useful for writing scrips and CLIs where you would like the user to see prettier content in their terminal.
38
38
 
39
39
  Supply a String argument for the content and any optional formatting arguments.
40
40
 
@@ -51,10 +51,20 @@ logp('Red alert!', color: :red, format: %i[underline bold italic])
51
51
  ```
52
52
 
53
53
  Avaliable optional keys are:
54
+
54
55
  - color
55
56
  - background
56
57
  - format
57
58
 
59
+ Additionally, you can specify a log file with an optional compatible Logger formatter proc.
60
+
61
+ ```
62
+ l = LogPretty::Logger.new('log/log_pretty.log', formatter)
63
+ l.logp("Hello, world! 👋")
64
+ ```
65
+
66
+ By default, LogPretty will write to the logging file in addition to STDOUT and return the output to the caller.
67
+
58
68
  ## License
59
69
 
60
70
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ # Extend module for global access to #logp method
6
+ module LogPretty
7
+ # Initialize a custom logger if setting the device or formatter
8
+ class Logger
9
+ def initialize(log_device = nil, formatter = nil, **_opts)
10
+ @log_device = log_device || $stdout
11
+ @formatter = formatter || default_formatter
12
+ end
13
+
14
+ # Will print to custom loggin device, if other than STDOUT,
15
+ # STDOUT by default, and returns output
16
+ def logp(input, **)
17
+ output = AnsiPrinter.new(input:, **).output
18
+
19
+ # print to logger
20
+ logger.info(output)
21
+
22
+ # print to $stdout unless it already is.
23
+ $stdout.puts(output) unless logger.instance_variable_get(:@logdev).dev == $stdout
24
+
25
+ # return value
26
+ output
27
+ end
28
+
29
+ private
30
+
31
+ def logger
32
+ @logger ||= ::Logger.new(@log_device).tap do |l|
33
+ l.formatter = @formatter
34
+ end
35
+ end
36
+
37
+ def default_formatter
38
+ proc do |_severity, _datetime, _progname, message|
39
+ "#{message}\n"
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LogPretty
4
- VERSION = '0.1.3'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/log-pretty.rb CHANGED
@@ -1,19 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'pry'
4
+ # frozen_string_literal: true
5
+
3
6
  require_relative 'log-pretty/version'
4
7
  require_relative 'log-pretty/ansi_printer'
8
+ require_relative 'log-pretty/logger'
5
9
 
6
10
  # Extend module for global access to #logp method
7
11
  module LogPretty
8
12
  class Error < StandardError; end
9
13
 
10
- def logp(input, **opts)
11
- output = AnsiPrinter.new(input:, **opts).output
12
-
13
- # Print to STDOUT
14
- $stdout.puts output
15
-
16
- # Return value
17
- output
14
+ # Stylize output simply with:
15
+ # import LogPretty
16
+ # logp('Hello, world! 👋', color: :teal, background: :blue, format: :bold)
17
+ #
18
+ # Or, set a custom log device and formatter:
19
+ #
20
+ # formatter = proc do |severity, datetime, progname, msg|
21
+ # "...#{msg}"
22
+ # end
23
+ #
24
+ # logger = LogPretty::Logger.new("pretty.log", formatter)
25
+ # logger.logp('Hello, world! 👋', color: red)
26
+ #
27
+ def logp(input, **)
28
+ Logger.new.logp(input, **)
18
29
  end
19
30
  end
metadata CHANGED
@@ -1,19 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log-pretty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesse vonBergen
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-09-27 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Provides intuitive methods for outputting ANSI escaped content to the
14
13
  terminal
15
14
  email:
16
- - jvon1904@gmail.com
15
+ - jvon1904@avocoaster.com
17
16
  executables: []
18
17
  extensions: []
19
18
  extra_rdoc_files: []
@@ -26,6 +25,7 @@ files:
26
25
  - Rakefile
27
26
  - lib/log-pretty.rb
28
27
  - lib/log-pretty/ansi_printer.rb
28
+ - lib/log-pretty/logger.rb
29
29
  - lib/log-pretty/version.rb
30
30
  - sig/log_pretty.rbs
31
31
  homepage: https://github.com/jvon1904/log-pretty
@@ -35,7 +35,6 @@ metadata:
35
35
  rubygems_mfa_required: 'true'
36
36
  homepage_uri: https://github.com/jvon1904/log-pretty
37
37
  source_code_uri: https://github.com/jvon1904/log-pretty
38
- post_install_message:
39
38
  rdoc_options: []
40
39
  require_paths:
41
40
  - lib
@@ -50,8 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
49
  - !ruby/object:Gem::Version
51
50
  version: '0'
52
51
  requirements: []
53
- rubygems_version: 3.5.20
54
- signing_key:
52
+ rubygems_version: 3.6.9
55
53
  specification_version: 4
56
54
  summary: Provides intuitive methods for outputting ANSI escaped content to the terminal
57
55
  test_files: []