使用Logger库-如何在Ruby中编写日志消息

本文介绍了在Ruby中使用内置的Logger库来记录日志的基本用法、消息优先级和日志轮换功能。通过设置不同的优先级,可以在运行时过滤不必要的日志信息,而在问题发生时获取更多细节。日志轮换则帮助保持日志文件大小适中,便于搜索历史记录。
摘要由CSDN通过智能技术生成

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.

Ruby中使用记录器库是跟踪代码何时出问题的一种简便方法。 当出现问题时,对导致错误的确切原因进行详细说明可以节省您查找错误的时间。 随着您的程序越来越大和越来越复杂,您可能希望添加一种写入日志消息的方法。 Ruby附带了许多有用的和库,称为标准库。 其中一个是记录器库,它提供了优先级和循环记录。

基本用法 ( Basic Usage )

Since the logger library comes with Ruby, there's 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.

由于记录器库是Ruby附带的,因此无需安装任何gems或其他库。 要开始使用记录器库,只需要求 “ logger”并创建一个新的Logger对象。 写入Logger对象的所有消息都将写入日志文件。


require 'logger'
需要“记录器”
log = Logger.new('log.txt')
日志= Logger.new('log.txt')
log.debug "Log file created"
log.debug“已创建日志文件”

优先事项 ( 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 they're 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.

每个日志消息都有一个优先级。 这些优先级使搜索日志文件中的严重消息变得很容易,并且使logger对象在不需要时自动过滤出较小的消息。 您可以认为它类似于您当天的待办事项清单。 有些事情是绝对必须做的,有些事情是真正应该做的,有些事情可以推迟,直到有时间去做。

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.

在上一个示例中,优先级是debug ,它是所有优先级中最不重要的(如果可以的话,“待办事项”列表中的“放开直到有时间”)。 日志消息的优先级从低到高依次为:调试,信息,警告, 错误和致命。 要设置记录器应该忽略的消息级别,请使用level属性。


require 'logger'
需要“记录器”
log = Logger.new('log.txt')
日志= Logger.new('log.txt')
log.level = Logger::WARN
log.level =记录器:: WARN
log.debug "This will be ignored"
log.debug“这将被忽略”
log.error "This will not be ignored"
log.error“这将不会被忽略”

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 you're 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.

要启用日志轮换,请将“每月”,“每周”或“每天”传递给Logger构造函数。 (可选)您可以传递最大文件大小和最大文件数,以将其轮流传递给构造函数。


require 'logger'
需要“记录器”
log = Logger.new( 'log.txt', 'daily' )
日志= Logger.new('log.txt','每日')
log.debug "Once the log becomes at least one"
log.debug“一旦日志变成至少一个”
log.debug "day old, it will be renamed and a"
log.debug“一天老了,它将被重命名并且一个”
log.debug "new log.txt file will be created."
log.debug“将创建新的log.txt文件。”

翻译自: https://www.thoughtco.com/write-log-messages-in-ruby-2908323

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值