如何从Grails应用程序访问日志目录内的文件? [英] how to access a file inside logs directory from grails application?

查看:97
本文介绍了如何从Grails应用程序访问日志目录内的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是试图在Grails的日志目录中打印最新日志文件的内容。 command.execute()。text正在返回空白。所以我一定在做错事。我感谢任何帮助!谢谢!

I am simply trying to print the content of the latest log file inside the logs directory from Grails. command.execute().text is returning empty. so I must be doing something wrong. I appreciate any help! Thanks!

def command = "cat \$(ls logs/localhost_access_log* | tail -1)"     

println command.execute().text


推荐答案

制作一个复杂的cmdline字符串,为什么不用groovy / java工具编程呢?

instead of making a complex cmdline string, why not just program it with the groovy/java tools?

// find real directory of logs folder
def dir = new File(ServletContextHolder.servletContext.getRealPath('logs'))
// inside logs folder look for newest file beginning with "localhost_access"
def file = dir.listFiles({ name -> name.startsWith('localhost_access') } as FilenameFilter).max({ it.lastModified() })
def content = null
if (file) {
    // read content directly
    def content = file.text
    // or via a process
    def content = new ProcessBuilder('cat', file.absolutePath).start().text
}

这篇关于如何从Grails应用程序访问日志目录内的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆