jjzjj

unix - log15 库将纪元时间从纳秒舍入到秒以进行日志记录

coder 2024-07-11 原文

我希望我的 golang 项目中的日志记录具有毫秒级的精度,目前使用的是 log15 库

问题是当我将它附加到 Context 时,它四舍五入到秒

newTimeWithMilliseconds := time.Unix(0, time.Now().UnixNano())

return log.FuncHandler(func(r *log.Record) {
     r.Ctx = append(r.Ctx, "time", newTimeWithMilliseconds)
     nextHandler.Log(r)
})

当我将它转换为字符串 newTimeWithMilliseconds.String() 时,它起作用了,我得到了 "2018-09-25 15:07:45.25801232 -0500 PDT",但是如果我不这样做,小数点就会消失,我得到 2018-09-25 15:07:45 -0700 PDT

我想保留时间格式而不使用字符串,有人知道为什么会发生这种情况以及如何解决这个问题吗?

最佳答案

您需要使用 time.Format以您想要的精度显示您的 time.Time 的值。除非你修改它的值,否则它不会失去它的精度,你看到差异的原因只是由于 time.Format 的调用所使用的 layout正在打印您的时间。

当您使用 mytime.String() 时,所使用的布局与其他打印时间的布局不同。

这就是为什么您需要使用 time.Time 值但操纵它的打印方式,或者将其存储为格式化字符串。

参见 Go by example

// Go supports time formatting and parsing via
// pattern-based layouts.

package main

import "fmt"
import "time"

func main() {
    p := fmt.Println

    // Here's a basic example of formatting a time
    // according to RFC3339, using the corresponding layout
    // constant.
    t := time.Now()
    p(t.Format(time.RFC3339))

    // Time parsing uses the same layout values as `Format`.
    t1, e := time.Parse(
        time.RFC3339,
        "2012-11-01T22:08:41+00:00")
    p(t1)

    // `Format` and `Parse` use example-based layouts. Usually
    // you'll use a constant from `time` for these layouts, but
    // you can also supply custom layouts. Layouts must use the
    // reference time `Mon Jan 2 15:04:05 MST 2006` to show the
    // pattern with which to format/parse a given time/string.
    // The example time must be exactly as shown: the year 2006,
    // 15 for the hour, Monday for the day of the week, etc.
    p(t.Format("3:04PM"))
    p(t.Format("Mon Jan _2 15:04:05 2006"))
    p(t.Format("2006-01-02T15:04:05.999999-07:00"))
    form := "3 04 PM"
    t2, e := time.Parse(form, "8 41 PM")
    p(t2)

    // For purely numeric representations you can also
    // use standard string formatting with the extracted
    // components of the time value.
    fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
        t.Year(), t.Month(), t.Day(),
        t.Hour(), t.Minute(), t.Second())

    // `Parse` will return an error on malformed input
    // explaining the parsing problem.
    ansic := "Mon Jan _2 15:04:05 2006"
    _, e = time.Parse(ansic, "8:41PM")
    p(e)
}

输出

2009-11-10T23:00:00Z
2012-11-01 22:08:41 +0000 UTC
11:00PM
Tue Nov 10 23:00:00 2009
2009-11-10T23:00:00+00:00
0000-01-01 20:41:00 +0000 UTC
2009-11-10T23:00:00-00:00
parsing time "8:41PM" as "Mon Jan _2 15:04:05 2006": cannot parse "8:41PM" as "Mon"

亲自尝试一下 here

关于unix - log15 库将纪元时间从纳秒舍入到秒以进行日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52507400/

有关unix - log15 库将纪元时间从纳秒舍入到秒以进行日志记录的更多相关文章

  1. ruby - Heroku production.log 文件位置 - 2

    我想在heroku.com上查看我的应用程序日志的内容,所以我关注了thisexcellentadvice并拥有我所有的日志内容。但是我现在很想知道我的日志文件实际在哪里,因为“log/production.log”似乎是空的:C:\>herokuconsoleRubyconsoleforajpbrevx.heroku.com>>files=Dir.glob("*")=>["public","tmp","spec","Rakefile","doc","config.ru","app","config","lib","README","Gemfile.lock","vendor","sc

  2. ruby-on-rails - 我需要一个真正的 UNIX RoR 开发环境 - 2

    从一开始,我就是一个Windows高手。我从MS-DOS开始。我安装了Windows2.1以及此后的所有Windows。现在,我家里有10台不同的Windows机器在运行,从Windows7Ultimate到各种版本的WindowsServer。我还没有完成Windows8,也不想去那里。我在服务器和各种软件方面都有UNIX经验,但它并不是我的首选环境。但是,我想我正在转换。我试图假装使用Cygwin和MSYS在Windows下运行UNIX。我的目的是搭建一个开发环境。两者都让我失望了。我花了比开发更多的时间来解决一系列技术问题。这是NotAcceptable。到目前为止,我的Ruby

  3. ruby - 如何使用 Ruby 中的 `less` 之类的 Unix 寻呼程序? - 2

    假设我有一个名为very_long_string的字符串,我想将其内容发送到标准输出。但是由于字符串很长,我想使用less在终端上显示文本。当我使用`less#{very_long_string}`我收到Filenotfound错误消息,如果我使用:`less我收到意外重定向错误消息。那么,如何在Ruby内部使用less呢? 最佳答案 您可以打开一个管道并通过其标准输入将您的字符串提供给less。IO.popen("less","w"){|f|f.putsvery_long_string}(假设very_long_string是保存

  4. ruby-on-rails - connect() 到 unix :/var/run/unicorn. 连接到上游时 sock 失败(111:连接被拒绝) - 2

    我遵循ruby​​onrails一个应用程序点击部署。数据库做得很好,即使我检查Rails控制台一切正常017/02/2615:34:17[error]18564#0:*31connect()tounix:/var/run/unicorn.sockfailed(111:Connectionrefused)whileconnectingtoupstream,client:121.52.156.57,server:_,request:"GET/HTTP/1.1",upstream:"http://unix:/var/run/unicorn.sock:/",host:"188.166.157

  5. ruby - ENOENT 在 Ruby 中创建 UNIX 套接字时 - 2

    我正在尝试使用Ruby创建套接字require"socket"w=UNIXSocket.new("socket")我不断遇到Nosuchfileordirectory-socket(Errno::ENOENT)这对我来说完全是倒退,因为new()应该创建那个丢失的文件。我错过了什么? 最佳答案 这太老了。请不要再尝试逐字使用它。http://blog.antarestrader.com/posts/153#!/rubyfile='path/to/my/socket'File.unlinkifFile.exists(file)&&Fi

  6. ruby-on-rails - Rails/Ruby 的痛苦 - 如何检查 gem 是否基于 UNIX/类 UNIX? - 2

    有什么方法可以查看gem是否仅在UNIX/类UNIX系统上受支持?是否有任何gem可以“筛选”所有gem并查看在Windows上使用它是否有任何问题。 最佳答案 简短回答:否。老实说,Windows在Ruby世界里是二等公民。这主要是因为Linux、BSD、OSX和几乎所有其他基于POSIX的系统都同意一件事,而Windows将去做完全不同的事情。即使是用于Windows的gem也可能偶尔会由于开发人员的疏忽而损坏。大多数gem作者没有针对Windows运行并依赖于用户错误报告的持续集成服务器。支持Windows很困难,不仅因为AP

  7. Python学习15:恺撒密码 B(python123) - 2

    描述恺撒密码是古罗马凯撒大帝用来对军事情报进行加解密的算法,它采用了替换方法对信息中的每一个英文字符循环替换为字母表序列中该字符后面的第三个字符,即,字母表的对应关系如下:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬原文:ABCDEFGHIJKLMNOPQRSTUVWXYZ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪

  8. ruby-on-rails - "554 Please activate your Mailgun account. Check your inbox or log in to your control panel to resend the activation email."错误 Ruby on Rails - 2

    我正在使用RubyonRails构建网络应用程序。我正在使用Mailgun作为这个应用程序的邮件程序。当我使用Facebook注册时它工作正常但是当我尝试使用电子邮件和密码注册时,我不断收到此错误“554请激活您的Mailgun帐户。检查您的收件箱或登录到您的控制面板以重新发送激活电子邮件。“我已经在mailgun仪表板中将eamil授权给授权收件人。这是我的代码:Registrations_controller.rbclassRegistrationsControllerconfig/environments/development.rbRails.application.confi

  9. ruby - Chromedriver `driver.manage.logs.get(:browser)` 在 chromedriver 75.0.3770.8 上失败 - 2

    在chromedriver75.0.3770.8上访问driver.manage.logs.get(:browser)-它导致错误#(NoMethodError)的未定义方法“日志”在74.0.3729.6上工作正常来自:https://github.com/SeleniumHQ/selenium/issues/7270 最佳答案 在最近的selenium-webdriver(4.4.0)和最近的Chrome(105)中,manage.logs不见了,但这有效:page.driver.browser.logs.get(:browse

  10. ruby - 是否有 Log4J for Ruby 的等价物,Log4Ruby? - 2

    找了一圈也没找到。是否有Ruby的Log4X等价物?如果不是,那么处理所有调试语句的最佳方法是什么。我是Ruby的新手。谢谢! 最佳答案 Ruby带有一个内置的日志库,但是有log4r.内置库的一个简短示例:#!/usr/bin/envrubyrequire'logger'log=Logger.new('mylog.txt')log.debug"Hellolog" 关于ruby-是否有Log4JforRuby的等价物,Log4Ruby?,我们在StackOverflow上找到一个类似的问

随机推荐