Monday, May 11, 2020
Using the Logger Library How to Write Logs in Ruby
Using the logger library in Ruby is an easy way to keep track of when something has gone wrong with your code. When something goes wrong, having a detailed account of exactly what happened leading up to the error can save you hours in locating the bug. As your programs get larger and more complex, you may want to add a way to write log messages. Ruby comes with a number of useful classes and libraries called the standard library. Among these is the logger library, which provides prioritized and rotated logging. Basic Usage Since the logger library comes with Ruby, theres no need to install any gems or other libraries. To begin using the logger library, simply require logger and create a new Logger object. Any messages written to the Logger object will be written to the log file. #!/usr/bin/env rubyrequire loggerlog Logger.new(log.txt)log.debug Log file created Priorities Each log message has a priority. These priorities make it simple to search log files for serious messages, as well as have the logger object automatically filter out lesser messages when theyre not needed. You can think of it sort of like your To Do list for the day. Some things absolutely must be done, some things really should get done, and some things can be put off until you have time to do them. In the previous example, the priority was debug, the least important of all the priorities (the put off until you have time of your To Do list, if you will). The log message priorities, in order from least to most important, are as follows: debug, info, warn, error, and fatal. To set the level of messages the logger should ignore, use the level attribute. #!/usr/bin/env rubyrequire loggerlog Logger.new(log.txt)log.level Logger::WARNlog.debug This will be ignoredlog.error This will not be ignored You can create as many log messages as you want and you can log every tiny little thing your program does, which makes priorities extremely useful. When youre running your program, you can leave the logger level on something like warn or error to catch the important stuff. Then, when something goes wrong, you can lower the logger level (either in the source code or with a command-line switch) to get more information. Rotation The logger library also supports log rotation. Log rotation keeps logs from getting too large and helps in searching through older logs. When log rotation is enabled and the log reaches either a certain size or a certain age, the logger library will rename that file and create a fresh log file. Older log files can also be configured to be deleted (or fall out of rotation) after a certain age. To enable log rotation, pass monthly, weekly, or daily to the Logger constructor. Optionally, you can pass a maximum file size and number of files to keep in rotation to the constructor. #!/usr/bin/env rubyrequire loggerlog Logger.new( log.txt, daily )log.debug Once the log becomes at least onelog.debug day old, it will be renamed and alog.debug new log.txt file will be created.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.