Scala 记录行号 - slf4s/slf4j?
更新:已解决!!!
请参阅我在下面添加的答案
有谁知道是否有一种方法可以进行日志记录,将真实的行号输出到控制台?我试图在这里进入 Scala,但是如果无法获得我所依赖的像这样的基本东西,那么真的很难取得进展。
我已经设置 slf4s 来用 slf4j - log4j - jcl-over-slf4j 包装。问题是我得到的行号根本不匹配。它们的行数甚至比 Scala 类包含的行数要高得多。这是因为行号实际上是 Java 中间行号吗?
是否有任何简单方法来设置满足这些要求的日志记录?:
- 可互操作,可与 java 和 java 一起使用。 scala
- 可以轻松地更改各个包的日志记录级别,就像 log4j
- 提供准确的行号一样容易。
谢谢!
杰米
UPDATE: SOLVED!!!
Please see the answer I added below
Does anyone know if there is a way to do logging that outputs real line numbers to the console? I'm trying to get into Scala here, but without being able to get basic stuff like this that I depend on, it's really hard to get much going.
I have set up slf4s to wrap with slf4j - log4j - jcl-over-slf4j. The problem is I get line numbers that do not match at all. The are much higher line numbers than the Scala class even contains. Is this because the line numbers are actually Java-intermediate line numbers?
Is there any EASY way to get set up logging that meets these requirements?:
- interoperable, working with both java & scala
- as easy to change the logging level of individual packages as easily as log4j
- provides ACCURATE line numbers.
Thanks!
Jamie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现
logback
(由 Ceki Gülcü)效果很好,并且也保留了行号!(它可以替代
log4j
:太棒了! )对于那些努力开始使用 Scala 的人,这就是我为获得基本框架所做的事情:
1)下载
sbt-launcher.jar
并将其放在/opt/
等位置我使用了“
sbt-launch-0.7.5.RC0.jar
"2) 创建一个 bash 脚本作为
nano/ 中 sbt 启动器的快捷方式opt/bin/sbt
:(使其可执行)
确保它也在您的路径中。
3)创建并配置 sbt 项目:
将其放在那里:
4)将上面的内容粘贴到 Main 中:
5)我差点忘了!将其放入
/src/main/resources/logback.xml
(需要获取行号)
6)
$ sbt
现在您应该位于 shell 的
sbt
控制台中:希望这会有所帮助。
I have found that
logback
(by Ceki Gülcü) works great and preserves line numbers too!(And it works as a replacement for
log4j
: Awesome!)For anyone struggling to get started with Scala, this is what I did to get the basic skeleton up:
1) Download
sbt-launcher.jar
and put it someplace like/opt/
I used "
sbt-launch-0.7.5.RC0.jar
"2) Create a bash script as a shortcut to the sbt launcher in
nano /opt/bin/sbt
:(make it executable)
Make sure it's in your path too.
3) Create and configure the sbt project:
put this in there:
4) Paste the stuff from above into Main:
5) I almost forgot! put this in
/src/main/resources/logback.xml
(it's required to get the line numbers)
6)
$ sbt
Now you should be in the
sbt
console in your shell:Hope this helps.
正如您在 Scala 日志记录 问题中评论时,在 Scala 中获取精确的行号信息很困难。
-g line
或-g vars
一起使用,(注:Scalate 报告在 scalate-24 用于不同类型的源文件)
As you are commenting in Scala logging question, getting precise line number information in Scala is hard.
-g line
or-g vars
(Note: Scalate reports having done a similar work in scalate-24 for a different kind of source files)
2016 年更新:像
lihaoyi/sourcecode
这样的库确实包含 日志用例采用新方法:您可以使用sourcecode.File和sourcecode.Line来定义自动捕获的日志函数他们的行号和文件名
Update 2016: a library like
lihaoyi/sourcecode
does include a logging use case with a new approach:You can use sourcecode.File and sourcecode.Line to define log functions that automatically capture their line number and file-name
我建议看看 Scribe。它是 Scala 的完整日志记录解决方案,使用宏在编译时生成行号和其他信息,因此不会降低速度,而且它是内置的,因此您不必使用诸如
sourcecode
并手动集成:https://github.com/outr/scribe
I would recommend taking a look at Scribe. It's a complete logging solution for Scala using Macros to generate line numbers and other information at compile-time so there is no reduction in speed, and it's built-in so you don't have to use something like
sourcecode
and integrate it manually:https://github.com/outr/scribe