CINXE.COM
class Logger - Documentation for Ruby 3.4
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>class Logger - Documentation for Ruby 3.4</title> <meta name="keywords" content="ruby,class,Logger"> <meta name="description" content="class Logger: Logging_(software)#Event_logs] for your program. Each such log contains a chronological sequence of entries that provides a record of the program's ac"> <script type="text/javascript"> var rdoc_rel_prefix = "./"; var index_rel_prefix = "./"; </script> <script src="./js/navigation.js" defer></script> <script src="./js/search.js" defer></script> <script src="./js/search_index.js" defer></script> <script src="./js/searcher.js" defer></script> <script src="./js/darkfish.js" defer></script> <link href="./css/fonts.css" rel="stylesheet"> <link href="./css/rdoc.css" rel="stylesheet"> <body id="top" role="document" class="class"> <div id="navigation-toggle" role="button" tabindex="0" aria-label="Toggle sidebar" aria-expanded="true" aria-controls="navigation"> <span aria-hidden="true">☰</span> </div> <nav id="navigation" role="navigation"> <div id="project-navigation"> <div id="home-section" role="region" title="Quick navigation" class="nav-section"> <h2> <a href="./index.html" rel="home">Home</a> </h2> <div id="table-of-contents-navigation"> <a href="./table_of_contents.html#pages">Pages</a> <a href="./table_of_contents.html#classes">Classes</a> <a href="./table_of_contents.html#methods">Methods</a> </div> </div> <div id="search-section" role="search" class="project-section initially-hidden"> <form action="#" method="get" accept-charset="utf-8"> <div id="search-field-wrapper"> <input id="search-field" role="combobox" aria-label="Search" aria-autocomplete="list" aria-controls="search-results" type="text" name="search" placeholder="Search (/) for a class, method, ..." spellcheck="false" title="Type to search, Up and Down to navigate, Enter to load"> </div> <ul id="search-results" aria-label="Search Results" aria-busy="false" aria-expanded="false" aria-atomic="false" class="initially-hidden"></ul> </form> </div> </div> <div class="nav-section"> <h3>Table of Contents</h3> <ul class="link-list" role="directory"> <li> <a href="#class-Logger-label-About+the+Examples">About the Examples</a> <li> <a href="#class-Logger-label-Synopsis">Synopsis</a> <li> <details open> <summary> <a href="#class-Logger-label-Entries">Entries</a> </summary> <ul class="link-list" role="directory"> <li> <a href="#class-Logger-label-Severity">Severity</a> <li> <a href="#class-Logger-label-Timestamp">Timestamp</a> <li> <a href="#class-Logger-label-Message">Message</a> <li> <a href="#class-Logger-label-Program+Name">Program Name</a> </ul> </details> </li> <li> <a href="#class-Logger-label-Log+Level">Log Level</a> <li> <details open> <summary> <a href="#class-Logger-label-Log+File+Rotation">Log File Rotation</a> </summary> <ul class="link-list" role="directory"> <li> <a href="#class-Logger-label-Size-Based+Rotation">Size-Based Rotation</a> <li> <a href="#class-Logger-label-Periodic+Rotation">Periodic Rotation</a> </ul> </details> </li> </ul> </div> <div id="parent-class-section" class="nav-section"> <h3>Ancestors</h3> <ul><li><a href="Object.html">Object</a><ul><li><a href="BasicObject.html">BasicObject</a></li></ul></li></ul> </div> <div id="includes-section" class="nav-section"> <h3>Included Modules</h3> <ul class="link-list"> <li><a class="include" href="Logger/Severity.html">Logger::Severity</a> </ul> </div> <div class="nav-section"> <h3>Class Methods</h3> <ul class="link-list" role="directory"> <li ><a href="#method-c-new">new</a></li> </ul> </div> <div class="nav-section"> <h3>Instance Methods</h3> <ul class="link-list" role="directory"> <li ><a href="#method-i-3C-3C"><<</a></li> <li ><a href="#method-i-add">add</a></li> <li ><a href="#method-i-close">close</a></li> <li ><a href="#method-i-datetime_format">datetime_format</a></li> <li ><a href="#method-i-datetime_format-3D">datetime_format=</a></li> <li ><a href="#method-i-debug">debug</a></li> <li ><a href="#method-i-debug-21">debug!</a></li> <li ><a href="#method-i-debug-3F">debug?</a></li> <li ><a href="#method-i-error">error</a></li> <li ><a href="#method-i-error-21">error!</a></li> <li ><a href="#method-i-error-3F">error?</a></li> <li ><a href="#method-i-fatal">fatal</a></li> <li ><a href="#method-i-fatal-21">fatal!</a></li> <li ><a href="#method-i-fatal-3F">fatal?</a></li> <li ><a href="#method-i-format_message">format_message</a></li> <li ><a href="#method-i-format_severity">format_severity</a></li> <li ><a href="#method-i-info">info</a></li> <li ><a href="#method-i-info-21">info!</a></li> <li ><a href="#method-i-info-3F">info?</a></li> <li ><a href="#method-i-level">level</a></li> <li ><a href="#method-i-level-3D">level=</a></li> <li ><a href="#method-i-level_key">level_key</a></li> <li ><a href="#method-i-level_override">level_override</a></li> <li ><a href="#method-i-log">log</a></li> <li ><a href="#method-i-reopen">reopen</a></li> <li ><a href="#method-i-sev_threshold">sev_threshold</a></li> <li ><a href="#method-i-sev_threshold-3D">sev_threshold=</a></li> <li ><a href="#method-i-unknown">unknown</a></li> <li ><a href="#method-i-warn">warn</a></li> <li ><a href="#method-i-warn-21">warn!</a></li> <li ><a href="#method-i-warn-3F">warn?</a></li> <li ><a href="#method-i-with_level">with_level</a></li> </ul> </div> <footer id="validator-badges" role="contentinfo"> <p><a href="https://validator.w3.org/check/referer">Validate</a> <p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.8.1. <p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>. </footer> </nav> <main role="main" aria-labelledby="class-Logger"> <h1 id="class-Logger" class="anchor-link class"> class Logger </h1> <section class="description"> <p>Class Logger provides a simple but sophisticated logging utility that you can use to create one or more <a href="https://en.wikipedia.org/wiki/Logging_(software)#Event_logs">event logs</a> for your program. Each such log contains a chronological sequence of entries that provides a record of the program’s activities.</p> <h2 id="class-Logger-label-About+the+Examples">About the Examples<span><a href="#class-Logger-label-About+the+Examples">¶</a> <a href="#top">↑</a></span></h2> <p>All examples on this page assume that Logger has been required:</p> <pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'logger'</span> </pre> <h2 id="class-Logger-label-Synopsis">Synopsis<span><a href="#class-Logger-label-Synopsis">¶</a> <a href="#top">↑</a></span></h2> <p>Create a log with <a href="Logger.html#method-c-new"><code>Logger.new</code></a>:</p> <pre class="ruby"><span class="ruby-comment"># Single log file.</span> <span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>) <span class="ruby-comment"># Size-based rotated logging: 3 10-megabyte files.</span> <span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>, <span class="ruby-value">3</span>, <span class="ruby-value">10485760</span>) <span class="ruby-comment"># Period-based rotated logging: daily (also allowed: 'weekly', 'monthly').</span> <span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>, <span class="ruby-string">'daily'</span>) <span class="ruby-comment"># Log to an IO stream.</span> <span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>) </pre> <p>Add entries (level, message) with <a href="Logger.html#method-i-add"><code>Logger#add</code></a>:</p> <pre class="ruby"><span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">DEBUG</span>, <span class="ruby-string">'Maximal debugging info'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">INFO</span>, <span class="ruby-string">'Non-error information'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">WARN</span>, <span class="ruby-string">'Non-error warning'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span>, <span class="ruby-string">'Non-fatal error'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">FATAL</span>, <span class="ruby-string">'Fatal error'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">UNKNOWN</span>, <span class="ruby-string">'Most severe'</span>) </pre> <p>Close the log with <a href="Logger.html#method-i-close"><code>Logger#close</code></a>:</p> <pre class="ruby"><span class="ruby-identifier">logger</span>.<span class="ruby-identifier">close</span> </pre> <h2 id="class-Logger-label-Entries">Entries<span><a href="#class-Logger-label-Entries">¶</a> <a href="#top">↑</a></span></h2> <p>You can add entries with method <a href="Logger.html#method-i-add"><code>Logger#add</code></a>:</p> <pre class="ruby"><span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">DEBUG</span>, <span class="ruby-string">'Maximal debugging info'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">INFO</span>, <span class="ruby-string">'Non-error information'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">WARN</span>, <span class="ruby-string">'Non-error warning'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span>, <span class="ruby-string">'Non-fatal error'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">FATAL</span>, <span class="ruby-string">'Fatal error'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">UNKNOWN</span>, <span class="ruby-string">'Most severe'</span>) </pre> <p>These shorthand methods also add entries:</p> <pre class="ruby"><span class="ruby-identifier">logger</span>.<span class="ruby-identifier">debug</span>(<span class="ruby-string">'Maximal debugging info'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">info</span>(<span class="ruby-string">'Non-error information'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">warn</span>(<span class="ruby-string">'Non-error warning'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">error</span>(<span class="ruby-string">'Non-fatal error'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">fatal</span>(<span class="ruby-string">'Fatal error'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">unknown</span>(<span class="ruby-string">'Most severe'</span>) </pre> <p>When you call any of these methods, the entry may or may not be written to the log, depending on the entry’s severity and on the log level; see <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a></p> <p>An entry always has:</p> <ul><li> <p>A severity (the required argument to <a href="Logger.html#method-i-add"><code>add</code></a>).</p> </li><li> <p>An automatically created timestamp.</p> </li></ul> <p>And may also have:</p> <ul><li> <p>A message.</p> </li><li> <p>A program name.</p> </li></ul> <p>Example:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">INFO</span>, <span class="ruby-string">'My message.'</span>, <span class="ruby-string">'mung'</span>) <span class="ruby-comment"># => I, [2022-05-07T17:21:46.536234 #20536] INFO -- mung: My message.</span> </pre> <p>The default format for an entry is:</p> <pre class="ruby"><span class="ruby-string">"%s, [%s #%d] %5s -- %s: %s\n"</span> </pre> <p>where the values to be formatted are:</p> <ul><li> <p>Severity (one letter).</p> </li><li> <p>Timestamp.</p> </li><li> <p><a href="Process.html"><code>Process</code></a> id.</p> </li><li> <p>Severity (word).</p> </li><li> <p>Program name.</p> </li><li> <p>Message.</p> </li></ul> <p>You can use a different entry format by:</p> <ul><li> <p>Setting a custom format proc (affects following entries); see <a href="Logger.html#attribute-i-formatter">formatter=</a>.</p> </li><li> <p>Calling any of the methods above with a block (affects only the one entry). Doing so can have two benefits:</p> <ul><li> <p>Context: the block can evaluate the entire program context and create a context-dependent message.</p> </li><li> <p>Performance: the block is not evaluated unless the log level permits the entry actually to be written:</p> <pre class="ruby"><span class="ruby-identifier">logger</span>.<span class="ruby-identifier">error</span> { <span class="ruby-identifier">my_slow_message_generator</span> } </pre> <p>Contrast this with the string form, where the string is always evaluated, regardless of the log level:</p> <pre class="ruby"><span class="ruby-identifier">logger</span>.<span class="ruby-identifier">error</span>(<span class="ruby-node">"#{my_slow_message_generator}"</span>) </pre> </li></ul> </li></ul> <h3 id="class-Logger-label-Severity">Severity<span><a href="#class-Logger-label-Severity">¶</a> <a href="#top">↑</a></span></h3> <p>The severity of a log entry has two effects:</p> <ul><li> <p>Determines whether the entry is selected for inclusion in the log; see <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a>.</p> </li><li> <p>Indicates to any log reader (whether a person or a program) the relative importance of the entry.</p> </li></ul> <h3 id="class-Logger-label-Timestamp">Timestamp<span><a href="#class-Logger-label-Timestamp">¶</a> <a href="#top">↑</a></span></h3> <p>The timestamp for a log entry is generated automatically when the entry is created.</p> <p>The logged timestamp is formatted by method <a href="Time.html#method-i-strftime"><code>Time#strftime</code></a> using this format string:</p> <pre class="ruby"><span class="ruby-string">'%Y-%m-%dT%H:%M:%S.%6N'</span> </pre> <p>Example:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">INFO</span>) <span class="ruby-comment"># => I, [2022-05-07T17:04:32.318331 #20536] INFO -- : nil</span> </pre> <p>You can set a different format using method <a href="Logger.html#method-i-datetime_format-3D"><code>datetime_format=</code></a>.</p> <h3 id="class-Logger-label-Message">Message<span><a href="#class-Logger-label-Message">¶</a> <a href="#top">↑</a></span></h3> <p>The message is an optional argument to an entry method:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">INFO</span>, <span class="ruby-string">'My message'</span>) <span class="ruby-comment"># => I, [2022-05-07T18:15:37.647581 #20536] INFO -- : My message</span> </pre> <p>For the default entry formatter, <code>Logger::Formatter</code>, the message object may be:</p> <ul><li> <p>A string: used as-is.</p> </li><li> <p>An Exception: <code>message.message</code> is used.</p> </li><li> <p>Anything else: <code>message.inspect</code> is used.</p> </li></ul> <p><strong>Note</strong>: <a href="Logger/Formatter.html"><code>Logger::Formatter</code></a> does not escape or sanitize the message passed to it. Developers should be aware that malicious data (user input) may be in the message, and should explicitly escape untrusted data.</p> <p>You can use a custom formatter to escape message data; see the example at <a href="Logger.html#attribute-i-formatter">formatter=</a>.</p> <h3 id="class-Logger-label-Program+Name">Program Name<span><a href="#class-Logger-label-Program+Name">¶</a> <a href="#top">↑</a></span></h3> <p>The program name is an optional argument to an entry method:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">INFO</span>, <span class="ruby-string">'My message'</span>, <span class="ruby-string">'mung'</span>) <span class="ruby-comment"># => I, [2022-05-07T18:17:38.084716 #20536] INFO -- mung: My message</span> </pre> <p>The default program name for a new logger may be set in the call to <a href="Logger.html#method-c-new"><code>Logger.new</code></a> via optional keyword argument <code>progname</code>:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>, <span class="ruby-value">progname:</span> <span class="ruby-string">'mung'</span>) </pre> <p>The default program name for an existing logger may be set by a call to method <a href="Logger.html#attribute-i-progname"><code>progname=</code></a>:</p> <pre class="ruby"><span class="ruby-identifier">logger</span>.<span class="ruby-identifier">progname</span> = <span class="ruby-string">'mung'</span> </pre> <p>The current program name may be retrieved with method <a href="Logger.html#attribute-i-progname">progname</a>:</p> <pre class="ruby"><span class="ruby-identifier">logger</span>.<span class="ruby-identifier">progname</span> <span class="ruby-comment"># => "mung"</span> </pre> <h2 id="class-Logger-label-Log+Level">Log Level<span><a href="#class-Logger-label-Log+Level">¶</a> <a href="#top">↑</a></span></h2> <p>The log level setting determines whether an entry is actually written to the log, based on the entry’s severity.</p> <p>These are the defined severities (least severe to most severe):</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">DEBUG</span>, <span class="ruby-string">'Maximal debugging info'</span>) <span class="ruby-comment"># => D, [2022-05-07T17:57:41.776220 #20536] DEBUG -- : Maximal debugging info</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">INFO</span>, <span class="ruby-string">'Non-error information'</span>) <span class="ruby-comment"># => I, [2022-05-07T17:59:14.349167 #20536] INFO -- : Non-error information</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">WARN</span>, <span class="ruby-string">'Non-error warning'</span>) <span class="ruby-comment"># => W, [2022-05-07T18:00:45.337538 #20536] WARN -- : Non-error warning</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span>, <span class="ruby-string">'Non-fatal error'</span>) <span class="ruby-comment"># => E, [2022-05-07T18:02:41.592912 #20536] ERROR -- : Non-fatal error</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">FATAL</span>, <span class="ruby-string">'Fatal error'</span>) <span class="ruby-comment"># => F, [2022-05-07T18:05:24.703931 #20536] FATAL -- : Fatal error</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">UNKNOWN</span>, <span class="ruby-string">'Most severe'</span>) <span class="ruby-comment"># => A, [2022-05-07T18:07:54.657491 #20536] ANY -- : Most severe</span> </pre> <p>The default initial level setting is Logger::DEBUG, the lowest level, which means that all entries are to be written, regardless of severity:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">level</span> <span class="ruby-comment"># => 0</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-value">0</span>, <span class="ruby-string">"My message"</span>) <span class="ruby-comment"># => D, [2022-05-11T15:10:59.773668 #20536] DEBUG -- : My message</span> </pre> <p>You can specify a different setting in a new logger using keyword argument <code>level</code> with an appropriate value:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>, <span class="ruby-value">level:</span> <span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span>) <span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>, <span class="ruby-value">level:</span> <span class="ruby-string">'error'</span>) <span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>, <span class="ruby-value">level:</span> <span class="ruby-value">:error</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">level</span> <span class="ruby-comment"># => 3</span> </pre> <p>With this level, entries with severity Logger::ERROR and higher are written, while those with lower severities are not written:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>, <span class="ruby-value">level:</span> <span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-value">3</span>) <span class="ruby-comment"># => E, [2022-05-11T15:17:20.933362 #20536] ERROR -- : nil</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-value">2</span>) <span class="ruby-comment"># Silent.</span> </pre> <p>You can set the log level for an existing logger with method <a href="Logger.html#method-i-level-3D"><code>level=</code></a>:</p> <pre class="ruby"><span class="ruby-identifier">logger</span>.<span class="ruby-identifier">level</span> = <span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span> </pre> <p>These shorthand methods also set the level:</p> <pre class="ruby"><span class="ruby-identifier">logger</span>.<span class="ruby-identifier">debug!</span> <span class="ruby-comment"># => 0</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">info!</span> <span class="ruby-comment"># => 1</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">warn!</span> <span class="ruby-comment"># => 2</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">error!</span> <span class="ruby-comment"># => 3</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">fatal!</span> <span class="ruby-comment"># => 4</span> </pre> <p>You can retrieve the log level with method <a href="Logger.html#method-i-level"><code>level</code></a>.</p> <pre class="ruby"><span class="ruby-identifier">logger</span>.<span class="ruby-identifier">level</span> = <span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">level</span> <span class="ruby-comment"># => 3</span> </pre> <p>These methods return whether a given level is to be written:</p> <pre class="ruby"><span class="ruby-identifier">logger</span>.<span class="ruby-identifier">level</span> = <span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">debug?</span> <span class="ruby-comment"># => false</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">info?</span> <span class="ruby-comment"># => false</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">warn?</span> <span class="ruby-comment"># => false</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">error?</span> <span class="ruby-comment"># => true</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">fatal?</span> <span class="ruby-comment"># => true</span> </pre> <h2 id="class-Logger-label-Log+File+Rotation">Log <a href="File.html"><code>File</code></a> Rotation<span><a href="#class-Logger-label-Log+File+Rotation">¶</a> <a href="#top">↑</a></span></h2> <p>By default, a log file is a single file that grows indefinitely (until explicitly closed); there is no file rotation.</p> <p>To keep log files to a manageable size, you can use <em>log</em> <em>file</em> <em>rotation</em>, which uses multiple log files:</p> <ul><li> <p>Each log file has entries for a non-overlapping time interval.</p> </li><li> <p>Only the most recent log file is open and active; the others are closed and inactive.</p> </li></ul> <h3 id="class-Logger-label-Size-Based+Rotation">Size-Based Rotation<span><a href="#class-Logger-label-Size-Based+Rotation">¶</a> <a href="#top">↑</a></span></h3> <p>For size-based log file rotation, call <a href="Logger.html#method-c-new"><code>Logger.new</code></a> with:</p> <ul><li> <p>Argument <code>logdev</code> as a file path.</p> </li><li> <p>Argument <code>shift_age</code> with a positive integer: the number of log files to be in the rotation.</p> </li><li> <p>Argument <code>shift_size</code> as a positive integer: the maximum size (in bytes) of each log file; defaults to 1048576 (1 megabyte).</p> </li></ul> <p>Examples:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>, <span class="ruby-value">3</span>) <span class="ruby-comment"># Three 1-megabyte files.</span> <span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>, <span class="ruby-value">5</span>, <span class="ruby-value">10485760</span>) <span class="ruby-comment"># Five 10-megabyte files.</span> </pre> <p>For these examples, suppose:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>, <span class="ruby-value">3</span>) </pre> <p>Logging begins in the new log file, <code>t.log</code>; the log file is “full” and ready for rotation when a new entry would cause its size to exceed <code>shift_size</code>.</p> <p>The first time <code>t.log</code> is full:</p> <ul><li> <p><code>t.log</code> is closed and renamed to <code>t.log.0</code>.</p> </li><li> <p>A new file <code>t.log</code> is opened.</p> </li></ul> <p>The second time <code>t.log</code> is full:</p> <ul><li> <p>+t.log.0 is renamed as <code>t.log.1</code>.</p> </li><li> <p><code>t.log</code> is closed and renamed to <code>t.log.0</code>.</p> </li><li> <p>A new file <code>t.log</code> is opened.</p> </li></ul> <p>Each subsequent time that <code>t.log</code> is full, the log files are rotated:</p> <ul><li> <p><code>t.log.1</code> is removed.</p> </li><li> <p>+t.log.0 is renamed as <code>t.log.1</code>.</p> </li><li> <p><code>t.log</code> is closed and renamed to <code>t.log.0</code>.</p> </li><li> <p>A new file <code>t.log</code> is opened.</p> </li></ul> <h3 id="class-Logger-label-Periodic+Rotation">Periodic Rotation<span><a href="#class-Logger-label-Periodic+Rotation">¶</a> <a href="#top">↑</a></span></h3> <p>For periodic rotation, call <a href="Logger.html#method-c-new"><code>Logger.new</code></a> with:</p> <ul><li> <p>Argument <code>logdev</code> as a file path.</p> </li><li> <p>Argument <code>shift_age</code> as a string period indicator.</p> </li></ul> <p>Examples:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>, <span class="ruby-string">'daily'</span>) <span class="ruby-comment"># Rotate log files daily.</span> <span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>, <span class="ruby-string">'weekly'</span>) <span class="ruby-comment"># Rotate log files weekly.</span> <span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>, <span class="ruby-string">'monthly'</span>) <span class="ruby-comment"># Rotate log files monthly.</span> </pre> <p>Example:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>, <span class="ruby-string">'daily'</span>) </pre> <p>When the given period expires:</p> <ul><li> <p>The base log file, <code>t.log</code> is closed and renamed with a date-based suffix such as <code>t.log.20220509</code>.</p> </li><li> <p>A new log file <code>t.log</code> is opened.</p> </li><li> <p>Nothing is removed.</p> </li></ul> <p>The default format for the suffix is <code>'%Y%m%d'</code>, which produces a suffix similar to the one above. You can set a different format using create-time option <code>shift_period_suffix</code>; see details and suggestions at <a href="Time.html#method-i-strftime"><code>Time#strftime</code></a>.</p> </section> <section id="5Buntitled-5D" class="documentation-section anchor-link"> <section class="constants-list"> <header> <h3>Constants</h3> </header> <dl> <dt id="ProgName">ProgName <dd> <dt id="SEV_LABEL">SEV_LABEL <dd> <p>Severity label for logging (max 5 chars).</p> <dt id="VERSION">VERSION <dd> </dl> </section> <section class="attribute-method-details" class="method-section"> <header> <h3>Attributes</h3> </header> <div id="attribute-i-formatter" class="method-detail anchor-link"> <div class="method-heading attribute-method-heading"> <a href="#attribute-i-formatter" title="Link to this attribute"> <span class="method-name">formatter</span> <span class="attribute-access-type">[RW]</span> </a> </div> <div class="method-description"> <p>Sets or retrieves the logger entry formatter proc.</p> <p>When <code>formatter</code> is <code>nil</code>, the logger uses <a href="Logger/Formatter.html"><code>Logger::Formatter</code></a>.</p> <p>When <code>formatter</code> is a proc, a new entry is formatted by the proc, which is called with four arguments:</p> <ul><li> <p><code>severity</code>: The severity of the entry.</p> </li><li> <p><code>time</code>: A <a href="Time.html"><code>Time</code></a> object representing the entry’s timestamp.</p> </li><li> <p><code>progname</code>: The program name for the entry.</p> </li><li> <p><code>msg</code>: The message for the entry (string or string-convertible object).</p> </li></ul> <p>The proc should return a string containing the formatted entry.</p> <p>This custom formatter uses <a href="String.html#method-i-dump"><code>String#dump</code></a> to escape the message string:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>, <span class="ruby-value">progname:</span> <span class="ruby-string">'mung'</span>) <span class="ruby-identifier">original_formatter</span> = <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">formatter</span> <span class="ruby-operator">||</span> <span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">Formatter</span>.<span class="ruby-identifier">new</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">formatter</span> = <span class="ruby-identifier">proc</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">severity</span>, <span class="ruby-identifier">time</span>, <span class="ruby-identifier">progname</span>, <span class="ruby-identifier">msg</span><span class="ruby-operator">|</span> <span class="ruby-identifier">original_formatter</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">severity</span>, <span class="ruby-identifier">time</span>, <span class="ruby-identifier">progname</span>, <span class="ruby-identifier">msg</span>.<span class="ruby-identifier">dump</span>) } <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">INFO</span>, <span class="ruby-string">"hello \n ''"</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">INFO</span>, <span class="ruby-string">"\f\x00\xff\\\""</span>) </pre> <p>Output:</p> <pre>I, [2022-05-13T13:16:29.637488 #8492] INFO -- mung: "hello \n ''" I, [2022-05-13T13:16:29.637610 #8492] INFO -- mung: "\f\x00\xFF\\\""</pre> </div> </div> <div id="attribute-i-progname" class="method-detail anchor-link"> <div class="method-heading attribute-method-heading"> <a href="#attribute-i-progname" title="Link to this attribute"> <span class="method-name">progname</span> <span class="attribute-access-type">[RW]</span> </a> </div> <div class="method-description"> <p>Program name to include in log messages.</p> </div> </div> </section> <section id="public-class-5Buntitled-5D-method-details" class="method-section anchor-link"> <header> <h3>Public Class Methods</h3> </header> <div id="method-c-new" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-c-new" title="Link to this method"> <span class="method-callseq"> new(logdev, shift_age = 0, shift_size = 1048576, **options) </span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="new-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 581</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">logdev</span>, <span class="ruby-identifier">shift_age</span> = <span class="ruby-value">0</span>, <span class="ruby-identifier">shift_size</span> = <span class="ruby-value">1048576</span>, <span class="ruby-value">level:</span> <span class="ruby-constant">DEBUG</span>, <span class="ruby-value">progname:</span> <span class="ruby-keyword">nil</span>, <span class="ruby-value">formatter:</span> <span class="ruby-keyword">nil</span>, <span class="ruby-value">datetime_format:</span> <span class="ruby-keyword">nil</span>, <span class="ruby-value">binmode:</span> <span class="ruby-keyword">false</span>, <span class="ruby-value">shift_period_suffix:</span> <span class="ruby-string">'%Y%m%d'</span>, <span class="ruby-value">reraise_write_errors:</span> []) <span class="ruby-keyword">self</span>.<span class="ruby-identifier">level</span> = <span class="ruby-identifier">level</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">progname</span> = <span class="ruby-identifier">progname</span> <span class="ruby-ivar">@default_formatter</span> = <span class="ruby-constant">Formatter</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">datetime_format</span> = <span class="ruby-identifier">datetime_format</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">formatter</span> = <span class="ruby-identifier">formatter</span> <span class="ruby-ivar">@logdev</span> = <span class="ruby-keyword">nil</span> <span class="ruby-ivar">@level_override</span> = {} <span class="ruby-keyword">if</span> <span class="ruby-identifier">logdev</span> <span class="ruby-operator">&&</span> <span class="ruby-identifier">logdev</span> <span class="ruby-operator">!=</span> <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">NULL</span> <span class="ruby-ivar">@logdev</span> = <span class="ruby-constant">LogDevice</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">logdev</span>, <span class="ruby-value">shift_age:</span> <span class="ruby-identifier">shift_age</span>, <span class="ruby-value">shift_size:</span> <span class="ruby-identifier">shift_size</span>, <span class="ruby-value">shift_period_suffix:</span> <span class="ruby-identifier">shift_period_suffix</span>, <span class="ruby-value">binmode:</span> <span class="ruby-identifier">binmode</span>, <span class="ruby-value">reraise_write_errors:</span> <span class="ruby-identifier">reraise_write_errors</span>) <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> <p>With the single argument <code>logdev</code>, returns a new logger with all default options:</p> <pre class="ruby"><span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>) <span class="ruby-comment"># => #<Logger:0x000001e685dc6ac8></span> </pre> <p>Argument <code>logdev</code> must be one of:</p> <ul><li> <p>A string filepath: entries are to be written to the file at that path; if the file at that path exists, new entries are appended.</p> </li><li> <p>An <a href="IO.html"><code>IO</code></a> stream (typically +$stdout+, +$stderr+. or an open file): entries are to be written to the given stream.</p> </li><li> <p><code>nil</code> or <code>File::NULL</code>: no entries are to be written.</p> </li></ul> <p>Examples:</p> <pre class="ruby"><span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>) <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>) </pre> <p>The keyword options are:</p> <ul><li> <p><code>level</code>: sets the log level; default value is Logger::DEBUG. See <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a>:</p> <pre class="ruby"><span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>, <span class="ruby-value">level:</span> <span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span>) </pre> </li><li> <p><code>progname</code>: sets the default program name; default is <code>nil</code>. See <a href="Logger.html#class-Logger-label-Program+Name">Program Name</a>:</p> <pre class="ruby"><span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>, <span class="ruby-value">progname:</span> <span class="ruby-string">'mung'</span>) </pre> </li><li> <p><code>formatter</code>: sets the entry formatter; default is <code>nil</code>. See <a href="Logger.html#attribute-i-formatter">formatter=</a>.</p> </li><li> <p><code>datetime_format</code>: sets the format for entry timestamp; default is <code>nil</code>. See <a href="Logger.html#method-i-datetime_format-3D"><code>datetime_format=</code></a>.</p> </li><li> <p><code>binmode</code>: sets whether the logger writes in binary mode; default is <code>false</code>.</p> </li><li> <p><code>shift_period_suffix</code>: sets the format for the filename suffix for periodic log file rotation; default is <code>'%Y%m%d'</code>. See <a href="Logger.html#class-Logger-label-Periodic+Rotation">Periodic Rotation</a>.</p> </li><li> <p><code>reraise_write_errors</code>: An array of exception classes, which will be reraised if there is an error when writing to the log device. The default is to swallow all exceptions raised.</p> </li></ul> </div> </div> </section> <section id="public-instance-5Buntitled-5D-method-details" class="method-section anchor-link"> <header> <h3>Public Instance Methods</h3> </header> <div id="method-i-3C-3C" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-3C-3C" title="Link to this method"> <span class="method-name"><<</span> <span class="method-args">(msg)</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="3C-3C-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 689</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title"><<</span>(<span class="ruby-identifier">msg</span>) <span class="ruby-ivar">@logdev</span>&.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">msg</span>) <span class="ruby-keyword">end</span></pre> </div> <p>Writes the given <code>msg</code> to the log with no formatting; returns the number of characters written, or <code>nil</code> if no log device exists:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>) <span class="ruby-identifier">logger</span> <span class="ruby-operator"><<</span> <span class="ruby-string">'My message.'</span> <span class="ruby-comment"># => 10</span> </pre> <p>Output:</p> <pre>My message.</pre> </div> </div> <div id="method-i-add" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-add" title="Link to this method"> <span class="method-name">add</span> <span class="method-args">(severity, message = nil, progname = nil) { || ... }</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="add-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 656</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">add</span>(<span class="ruby-identifier">severity</span>, <span class="ruby-identifier">message</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">progname</span> = <span class="ruby-keyword">nil</span>) <span class="ruby-identifier">severity</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">UNKNOWN</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@logdev</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword">or</span> <span class="ruby-identifier">severity</span> <span class="ruby-operator"><</span> <span class="ruby-identifier">level</span> <span class="ruby-keyword">return</span> <span class="ruby-keyword">true</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">progname</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-identifier">progname</span> = <span class="ruby-ivar">@progname</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">message</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span> <span class="ruby-identifier">message</span> = <span class="ruby-keyword">yield</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">message</span> = <span class="ruby-identifier">progname</span> <span class="ruby-identifier">progname</span> = <span class="ruby-ivar">@progname</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span> <span class="ruby-ivar">@logdev</span>.<span class="ruby-identifier">write</span>( <span class="ruby-identifier">format_message</span>(<span class="ruby-identifier">format_severity</span>(<span class="ruby-identifier">severity</span>), <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>, <span class="ruby-identifier">progname</span>, <span class="ruby-identifier">message</span>)) <span class="ruby-keyword">true</span> <span class="ruby-keyword">end</span></pre> </div> <p>Creates a log entry, which may or may not be written to the log, depending on the entry’s severity and on the log level. See <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a> and <a href="Logger.html#class-Logger-label-Entries">Entries</a> for details.</p> <p>Examples:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">$stdout</span>, <span class="ruby-value">progname:</span> <span class="ruby-string">'mung'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">INFO</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span>, <span class="ruby-string">'No good'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span>, <span class="ruby-string">'No good'</span>, <span class="ruby-string">'gnum'</span>) </pre> <p>Output:</p> <pre>I, [2022-05-12T16:25:31.469726 #36328] INFO -- mung: mung E, [2022-05-12T16:25:55.349414 #36328] ERROR -- mung: No good E, [2022-05-12T16:26:35.841134 #36328] ERROR -- gnum: No good</pre> <p>These convenience methods have implicit severity:</p> <ul><li> <p><a href="Logger.html#method-i-debug"><code>debug</code></a>.</p> </li><li> <p><a href="Logger.html#method-i-info"><code>info</code></a>.</p> </li><li> <p><a href="Logger.html#method-i-warn"><code>warn</code></a>.</p> </li><li> <p><a href="Logger.html#method-i-error"><code>error</code></a>.</p> </li><li> <p><a href="Logger.html#method-i-fatal"><code>fatal</code></a>.</p> </li><li> <p><a href="Logger.html#method-i-unknown"><code>unknown</code></a>.</p> </li></ul> </div> <div class="aliases"> Also aliased as: <a href="Logger.html#method-i-log">log</a> </div> </div> <div id="method-i-close" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-close" title="Link to this method"> <span class="method-name">close</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="close-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 736</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">close</span> <span class="ruby-ivar">@logdev</span>&.<span class="ruby-identifier">close</span> <span class="ruby-keyword">end</span></pre> </div> <p>Closes the logger; returns <code>nil</code>:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">close</span> <span class="ruby-comment"># => nil</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">info</span>(<span class="ruby-string">'foo'</span>) <span class="ruby-comment"># Prints "log writing failed. closed stream"</span> </pre> <p>Related: <a href="Logger.html#method-i-reopen"><code>Logger#reopen</code></a>.</p> </div> </div> <div id="method-i-datetime_format" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-datetime_format" title="Link to this method"> <span class="method-name">datetime_format</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="datetime_format-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 438</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">datetime_format</span> <span class="ruby-ivar">@default_formatter</span>.<span class="ruby-identifier">datetime_format</span> <span class="ruby-keyword">end</span></pre> </div> <p>Returns the date-time format; see <a href="Logger.html#method-i-datetime_format-3D"><code>datetime_format=</code></a>.</p> </div> </div> <div id="method-i-datetime_format-3D" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-datetime_format-3D" title="Link to this method"> <span class="method-name">datetime_format=</span> <span class="method-args">(datetime_format)</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="datetime_format-3D-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 432</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">datetime_format=</span>(<span class="ruby-identifier">datetime_format</span>) <span class="ruby-ivar">@default_formatter</span>.<span class="ruby-identifier">datetime_format</span> = <span class="ruby-identifier">datetime_format</span> <span class="ruby-keyword">end</span></pre> </div> <p>Sets the date-time format.</p> <p>Argument <code>datetime_format</code> should be either of these:</p> <ul><li> <p>A string suitable for use as a format for method <a href="Time.html#method-i-strftime"><code>Time#strftime</code></a>.</p> </li><li> <p><code>nil</code>: the logger uses <code>'%Y-%m-%dT%H:%M:%S.%6N'</code>.</p> </li></ul> </div> </div> <div id="method-i-debug" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-debug" title="Link to this method"> <span class="method-name">debug</span> <span class="method-args">(progname = nil, &block)</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="debug-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 695</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">debug</span>(<span class="ruby-identifier">progname</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">add</span>(<span class="ruby-constant">DEBUG</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">progname</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">end</span></pre> </div> <p>Equivalent to calling <a href="Logger.html#method-i-add"><code>add</code></a> with severity <code>Logger::DEBUG</code>.</p> </div> </div> <div id="method-i-debug-21" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-debug-21" title="Link to this method"> <span class="method-name">debug!</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="debug-21-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 487</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">debug!</span>; <span class="ruby-keyword">self</span>.<span class="ruby-identifier">level</span> = <span class="ruby-constant">DEBUG</span>; <span class="ruby-keyword">end</span></pre> </div> <p>Sets the log level to Logger::DEBUG. See <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a>.</p> </div> </div> <div id="method-i-debug-3F" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-debug-3F" title="Link to this method"> <span class="method-name">debug?</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="debug-3F-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 482</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">debug?</span>; <span class="ruby-identifier">level</span> <span class="ruby-operator"><=</span> <span class="ruby-constant">DEBUG</span>; <span class="ruby-keyword">end</span></pre> </div> <p>Returns <code>true</code> if the log level allows entries with severity Logger::DEBUG to be written, <code>false</code> otherwise. See <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a>.</p> </div> </div> <div id="method-i-error" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-error" title="Link to this method"> <span class="method-name">error</span> <span class="method-args">(progname = nil, &block)</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="error-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 713</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">error</span>(<span class="ruby-identifier">progname</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">add</span>(<span class="ruby-constant">ERROR</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">progname</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">end</span></pre> </div> <p>Equivalent to calling <a href="Logger.html#method-i-add"><code>add</code></a> with severity <code>Logger::ERROR</code>.</p> </div> </div> <div id="method-i-error-21" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-error-21" title="Link to this method"> <span class="method-name">error!</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="error-21-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 520</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">error!</span>; <span class="ruby-keyword">self</span>.<span class="ruby-identifier">level</span> = <span class="ruby-constant">ERROR</span>; <span class="ruby-keyword">end</span></pre> </div> <p>Sets the log level to Logger::ERROR. See <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a>.</p> </div> </div> <div id="method-i-error-3F" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-error-3F" title="Link to this method"> <span class="method-name">error?</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="error-3F-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 515</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">error?</span>; <span class="ruby-identifier">level</span> <span class="ruby-operator"><=</span> <span class="ruby-constant">ERROR</span>; <span class="ruby-keyword">end</span></pre> </div> <p>Returns <code>true</code> if the log level allows entries with severity Logger::ERROR to be written, <code>false</code> otherwise. See <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a>.</p> </div> </div> <div id="method-i-fatal" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-fatal" title="Link to this method"> <span class="method-name">fatal</span> <span class="method-args">(progname = nil, &block)</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="fatal-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 719</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">fatal</span>(<span class="ruby-identifier">progname</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">add</span>(<span class="ruby-constant">FATAL</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">progname</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">end</span></pre> </div> <p>Equivalent to calling <a href="Logger.html#method-i-add"><code>add</code></a> with severity <code>Logger::FATAL</code>.</p> </div> </div> <div id="method-i-fatal-21" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-fatal-21" title="Link to this method"> <span class="method-name">fatal!</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="fatal-21-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 531</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">fatal!</span>; <span class="ruby-keyword">self</span>.<span class="ruby-identifier">level</span> = <span class="ruby-constant">FATAL</span>; <span class="ruby-keyword">end</span></pre> </div> <p>Sets the log level to Logger::FATAL. See <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a>.</p> </div> </div> <div id="method-i-fatal-3F" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-fatal-3F" title="Link to this method"> <span class="method-name">fatal?</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="fatal-3F-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 526</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">fatal?</span>; <span class="ruby-identifier">level</span> <span class="ruby-operator"><=</span> <span class="ruby-constant">FATAL</span>; <span class="ruby-keyword">end</span></pre> </div> <p>Returns <code>true</code> if the log level allows entries with severity Logger::FATAL to be written, <code>false</code> otherwise. See <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a>.</p> </div> </div> <div id="method-i-info" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-info" title="Link to this method"> <span class="method-name">info</span> <span class="method-args">(progname = nil, &block)</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="info-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 701</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">info</span>(<span class="ruby-identifier">progname</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">add</span>(<span class="ruby-constant">INFO</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">progname</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">end</span></pre> </div> <p>Equivalent to calling <a href="Logger.html#method-i-add"><code>add</code></a> with severity <code>Logger::INFO</code>.</p> </div> </div> <div id="method-i-info-21" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-info-21" title="Link to this method"> <span class="method-name">info!</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="info-21-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 498</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">info!</span>; <span class="ruby-keyword">self</span>.<span class="ruby-identifier">level</span> = <span class="ruby-constant">INFO</span>; <span class="ruby-keyword">end</span></pre> </div> <p>Sets the log level to Logger::INFO. See <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a>.</p> </div> </div> <div id="method-i-info-3F" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-info-3F" title="Link to this method"> <span class="method-name">info?</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="info-3F-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 493</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">info?</span>; <span class="ruby-identifier">level</span> <span class="ruby-operator"><=</span> <span class="ruby-constant">INFO</span>; <span class="ruby-keyword">end</span></pre> </div> <p>Returns <code>true</code> if the log level allows entries with severity Logger::INFO to be written, <code>false</code> otherwise. See <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a>.</p> </div> </div> <div id="method-i-level" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-level" title="Link to this method"> <span class="method-name">level</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="level-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 383</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">level</span> <span class="ruby-identifier">level_override</span>[<span class="ruby-identifier">level_key</span>] <span class="ruby-operator">||</span> <span class="ruby-ivar">@level</span> <span class="ruby-keyword">end</span></pre> </div> <p>Logging severity threshold (e.g. <code>Logger::INFO</code>).</p> </div> <div class="aliases"> Also aliased as: <a href="Logger.html#method-i-sev_threshold">sev_threshold</a> </div> </div> <div id="method-i-level-3D" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-level-3D" title="Link to this method"> <span class="method-name">level=</span> <span class="method-args">(severity)</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="level-3D-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 399</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">level=</span>(<span class="ruby-identifier">severity</span>) <span class="ruby-ivar">@level</span> = <span class="ruby-constant">Severity</span>.<span class="ruby-identifier">coerce</span>(<span class="ruby-identifier">severity</span>) <span class="ruby-keyword">end</span></pre> </div> <p>Sets the log level; returns <code>severity</code>. See <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a>.</p> <p>Argument <code>severity</code> may be an integer, a string, or a symbol:</p> <pre class="ruby"><span class="ruby-identifier">logger</span>.<span class="ruby-identifier">level</span> = <span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span> <span class="ruby-comment"># => 3</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">level</span> = <span class="ruby-value">3</span> <span class="ruby-comment"># => 3</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">level</span> = <span class="ruby-string">'error'</span> <span class="ruby-comment"># => "error"</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">level</span> = <span class="ruby-value">:error</span> <span class="ruby-comment"># => :error</span> </pre> <p><a href="Logger.html#method-i-sev_threshold-3D"><code>Logger#sev_threshold=</code></a> is an alias for <a href="Logger.html#method-i-level-3D"><code>Logger#level=</code></a>.</p> </div> <div class="aliases"> Also aliased as: <a href="Logger.html#method-i-sev_threshold-3D">sev_threshold=</a> </div> </div> <div id="method-i-log" class="method-detail anchor-link method-alias"> <div class="method-header"> <div class="method-heading"> <a href="#method-i-log" title="Link to this method"> <span class="method-name">log</span> <span class="method-args">(severity, message = nil, progname = nil)</span> </a> </div> </div> <div class="method-description"> </div> <div class="aliases"> Alias for: <a href="Logger.html#method-i-add">add</a> </div> </div> <div id="method-i-reopen" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-reopen" title="Link to this method"> <span class="method-name">reopen</span> <span class="method-args">(logdev = nil)</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="reopen-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 624</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">reopen</span>(<span class="ruby-identifier">logdev</span> = <span class="ruby-keyword">nil</span>) <span class="ruby-ivar">@logdev</span>&.<span class="ruby-identifier">reopen</span>(<span class="ruby-identifier">logdev</span>) <span class="ruby-keyword">self</span> <span class="ruby-keyword">end</span></pre> </div> <p>Sets the logger’s output stream:</p> <ul><li> <p>If <code>logdev</code> is <code>nil</code>, reopens the current output stream.</p> </li><li> <p>If <code>logdev</code> is a filepath, opens the indicated file for append.</p> </li><li> <p>If <code>logdev</code> is an <a href="IO.html"><code>IO</code></a> stream (usually <code>$stdout</code>, <code>$stderr</code>, or an open <a href="File.html"><code>File</code></a> object), opens the stream for append.</p> </li></ul> <p>Example:</p> <pre class="ruby"><span class="ruby-identifier">logger</span> = <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">'t.log'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span>, <span class="ruby-string">'one'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">close</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span>, <span class="ruby-string">'two'</span>) <span class="ruby-comment"># Prints 'log writing failed. closed stream'</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">reopen</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">add</span>(<span class="ruby-constant">Logger</span><span class="ruby-operator">::</span><span class="ruby-constant">ERROR</span>, <span class="ruby-string">'three'</span>) <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">close</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">readlines</span>(<span class="ruby-string">'t.log'</span>) <span class="ruby-comment"># =></span> <span class="ruby-comment"># ["# Logfile created on 2022-05-12 14:21:19 -0500 by logger.rb/v1.5.0\n",</span> <span class="ruby-comment"># "E, [2022-05-12T14:21:27.596726 #22428] ERROR -- : one\n",</span> <span class="ruby-comment"># "E, [2022-05-12T14:23:05.847241 #22428] ERROR -- : three\n"]</span> </pre> </div> </div> <div id="method-i-sev_threshold" class="method-detail anchor-link method-alias"> <div class="method-header"> <div class="method-heading"> <a href="#method-i-sev_threshold" title="Link to this method"> <span class="method-name">sev_threshold</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-description"> </div> <div class="aliases"> Alias for: <a href="Logger.html#method-i-level">level</a> </div> </div> <div id="method-i-sev_threshold-3D" class="method-detail anchor-link method-alias"> <div class="method-header"> <div class="method-heading"> <a href="#method-i-sev_threshold-3D" title="Link to this method"> <span class="method-name">sev_threshold=</span> <span class="method-args">(severity)</span> </a> </div> </div> <div class="method-description"> </div> <div class="aliases"> Alias for: <a href="Logger.html#method-i-level-3D">level=</a> </div> </div> <div id="method-i-unknown" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-unknown" title="Link to this method"> <span class="method-name">unknown</span> <span class="method-args">(progname = nil, &block)</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="unknown-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 725</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">unknown</span>(<span class="ruby-identifier">progname</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">add</span>(<span class="ruby-constant">UNKNOWN</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">progname</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">end</span></pre> </div> <p>Equivalent to calling <a href="Logger.html#method-i-add"><code>add</code></a> with severity <code>Logger::UNKNOWN</code>.</p> </div> </div> <div id="method-i-warn" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-warn" title="Link to this method"> <span class="method-name">warn</span> <span class="method-args">(progname = nil, &block)</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="warn-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 707</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">warn</span>(<span class="ruby-identifier">progname</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) <span class="ruby-identifier">add</span>(<span class="ruby-constant">WARN</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">progname</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">end</span></pre> </div> <p>Equivalent to calling <a href="Logger.html#method-i-add"><code>add</code></a> with severity <code>Logger::WARN</code>.</p> </div> </div> <div id="method-i-warn-21" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-warn-21" title="Link to this method"> <span class="method-name">warn!</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="warn-21-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 509</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">warn!</span>; <span class="ruby-keyword">self</span>.<span class="ruby-identifier">level</span> = <span class="ruby-constant">WARN</span>; <span class="ruby-keyword">end</span></pre> </div> <p>Sets the log level to Logger::WARN. See <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a>.</p> </div> </div> <div id="method-i-warn-3F" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-warn-3F" title="Link to this method"> <span class="method-name">warn?</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="warn-3F-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 504</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">warn?</span>; <span class="ruby-identifier">level</span> <span class="ruby-operator"><=</span> <span class="ruby-constant">WARN</span>; <span class="ruby-keyword">end</span></pre> </div> <p>Returns <code>true</code> if the log level allows entries with severity Logger::WARN to be written, <code>false</code> otherwise. See <a href="Logger.html#class-Logger-label-Log+Level">Log Level</a>.</p> </div> </div> <div id="method-i-with_level" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-with_level" title="Link to this method"> <span class="method-name">with_level</span> <span class="method-args">(severity) { || ... }</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="with_level-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 408</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">with_level</span>(<span class="ruby-identifier">severity</span>) <span class="ruby-identifier">prev</span>, <span class="ruby-identifier">level_override</span>[<span class="ruby-identifier">level_key</span>] = <span class="ruby-identifier">level</span>, <span class="ruby-constant">Severity</span>.<span class="ruby-identifier">coerce</span>(<span class="ruby-identifier">severity</span>) <span class="ruby-keyword">begin</span> <span class="ruby-keyword">yield</span> <span class="ruby-keyword">ensure</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">prev</span> <span class="ruby-identifier">level_override</span>[<span class="ruby-identifier">level_key</span>] = <span class="ruby-identifier">prev</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">level_override</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">level_key</span>) <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span> <span class="ruby-keyword">end</span></pre> </div> <p>Adjust the log level during the block execution for the current <a href="Fiber.html"><code>Fiber</code></a> only</p> <pre class="ruby"><span class="ruby-identifier">logger</span>.<span class="ruby-identifier">with_level</span>(<span class="ruby-value">:debug</span>) <span class="ruby-keyword">do</span> <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">debug</span> { <span class="ruby-string">"Hello"</span> } <span class="ruby-keyword">end</span> </pre> </div> </div> </section> <section id="private-instance-5Buntitled-5D-method-details" class="method-section anchor-link"> <header> <h3>Private Instance Methods</h3> </header> <div id="method-i-format_message" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-format_message" title="Link to this method"> <span class="method-name">format_message</span> <span class="method-args">(severity, datetime, progname, msg)</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="format_message-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 758</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">format_message</span>(<span class="ruby-identifier">severity</span>, <span class="ruby-identifier">datetime</span>, <span class="ruby-identifier">progname</span>, <span class="ruby-identifier">msg</span>) (<span class="ruby-ivar">@formatter</span> <span class="ruby-operator">||</span> <span class="ruby-ivar">@default_formatter</span>).<span class="ruby-identifier">call</span>(<span class="ruby-identifier">severity</span>, <span class="ruby-identifier">datetime</span>, <span class="ruby-identifier">progname</span>, <span class="ruby-identifier">msg</span>) <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-format_severity" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-format_severity" title="Link to this method"> <span class="method-name">format_severity</span> <span class="method-args">(severity)</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="format_severity-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 745</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">format_severity</span>(<span class="ruby-identifier">severity</span>) <span class="ruby-constant">SEV_LABEL</span>[<span class="ruby-identifier">severity</span>] <span class="ruby-operator">||</span> <span class="ruby-string">'ANY'</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-level_key" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-level_key" title="Link to this method"> <span class="method-name">level_key</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="level_key-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 754</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">level_key</span> <span class="ruby-constant">Fiber</span>.<span class="ruby-identifier">current</span> <span class="ruby-keyword">end</span></pre> </div> </div> </div> <div id="method-i-level_override" class="method-detail anchor-link "> <div class="method-header"> <div class="method-heading"> <a href="#method-i-level_override" title="Link to this method"> <span class="method-name">level_override</span> <span class="method-args">()</span> </a> </div> </div> <div class="method-controls"> <details class="method-source-toggle"> <summary>Source</summary> </details> </div> <div class="method-description"> <div class="method-source-code" id="level_override-source"> <pre><span class="ruby-comment"># File lib/logger.rb, line 750</span> <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">level_override</span> <span class="ruby-ivar">@level_override</span> <span class="ruby-operator">||=</span> {} <span class="ruby-keyword">end</span></pre> </div> <p>Guarantee the existence of this ivar even when subclasses don’t call the superclass constructor.</p> </div> </div> </section> </section> </main>