使用Groovy或Java

时间:2015-11-19 14:57:22

标签: java logging groovy log4j syslog

日志来自POSTGRESQL,我也使用Script Runner来实现这一点。我一直在寻找将这些日志结束到syslog服务器的方法,因为我真的很新,我不知道从哪里开始。

我们说这是服务器的名称: syslog12 和端口: 514

如果有人可以教我如何将日志发送到syslog服务器,我真的很感激。

这是我的常规代码:

return getUserId()

class Logs{
                    String id = ""
                    String created = ""
                    String summary =""
                    String category =""
                    String searchField =""
     String toString(){
        "$id ==== $created-$summary,$category,$searchField"    
     }
}

def getUserId(){
        def driver = Class.forName('org.postgresql.Driver').newInstance() as Driver
        def props = new Properties()

        props.setProperty("user", "USERNAME")
        props.setProperty("password", "PASSWORD")
        props.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory")
        props.setProperty("ssl", "true")

        def conn = driver.connect("jdbc:postgresql://DATABASE:PORT/GRP", props)
        def sql = new Sql(conn)

        try {

                BufferedWriter outputFile = new BufferedWriter(new FileWriter("/GRP/atlassian/testProd1.txt", true))
                def logs = new Logs()
                String query = "SELECT * from audit_log where created >='2015-04-15' AND created < '2015-04-26' order by id ASC"

                PreparedStatement statement = conn.prepareStatement(query)

                ResultSet result = statement.executeQuery()

                while(result.next()){
                        String id1 = result.getString("id")
                        logs.id = id1

                        String created1 = result.getString("created")
                        logs.created = created1

                        String summary1 = result.getString("summary")
                        logs.summary = summary1

                        String category1 = result.getString("category")
                        logs.category = category1

                        String searchField1 = result.getString("search_field")
                        logs.searchField = searchField1

                        outputFile.write("[GRP2.0] "+logs.toString())
                        outputFile.newLine()
                }

                outputFile.close()
                return ("[GRP2.0] "+logs.toString())
        } finally {
            sql.close()
            conn.close()
        }

}

1 个答案:

答案 0 :(得分:0)

Logback非常方便syslog appender。因为它可以是configured with groovy,所以一个简单的例子非常简单:

<强> Example.groovy

package com.jalopaba.syslog

import org.slf4j.Logger
import org.slf4j.LoggerFactory

class Example {
    private static final Logger LOG = LoggerFactory.getLogger(Example.class)

    static void main(args) {
        LOG.info('Test message: ' + new Date())
    }
}

<强> logback.groovy

appender('console', ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        pattern = "%d{HH:mm:ss.SSS} %-5level [%thread] - %msg%n"
    }
}

appender('syslog', SyslogAppender) {
    syslogHost = 'remote_host'
    facility = 'USER'
    suffixPattern = "%thread: %-5level %logger{36} - %msg%n"
}

logger('com.jalopaba', DEBUG, ['console', 'syslog'])

那样:

remote_host:~$ tail -f /var/log/syslog
Nov 20 12:35:58 jalopaba_machine main: INFO  com.jalopaba.syslog.Example - Test message: Fri Nov 20 12:35:58 CET 2015#015

请记住, syslog / rsyslog 通常默认情况下未启用远程日志记录:What's wrong with my logback syslog appender?