Command Line

This guide explains how the console gem can be controlled using environment variables.

Environment Variables

The following program, app.rb will be used to explain the different environmet variables:

~~~ ruby

#!/usr/bin/env ruby

require ‘console’

class MyApplication def do_work Console.logger.debug(self, “Doing some work.”) end end

Console.logger.debug(self, “Creating the application.”) application = MyApplication.new application.do_work ~~~

CONSOLE_LEVEL=debug

Control the default logger level. You can set it to any of the supported log levels: debug, info, warn, error, fatal.

By default, the debug level is not enabled, but you can enable it using CONSOLE_LEVEL=debug:

> CONSOLE_LEVEL=debug ./app.rb
  0.0s    debug: Object [oid=0x3c] [ec=0x50] [pid=990900] [2022-10-12 17:28:15 +1300]
               | Creating the application.
  0.0s    debug: MyApplication [oid=0x64] [ec=0x50] [pid=990900] [2022-10-12 17:28:15 +1300]
               | Doing some work.

CONSOLE_DEBUG=MyClass,MyModule::MyClass

Enable debug logging for the specified class names. You can specify one or more class names which will be resolved at runtime. This is specifically related to subject logging.

By default, the debug level is not enabled, but you can enable it for the specific MyApplication class using CONSOLE_DEBUG=MyApplication:

> CONSOLE_DEBUG=MyApplication ./app.rb
  0.0s    debug: MyApplication [oid=0x64] [ec=0x78] [pid=991855] [2022-10-12 17:30:56 +1300]
               | Doing some work.