首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Jenkins Blue Ocean中以短格式显示时间戳?

如何在Jenkins Blue Ocean中以短格式显示时间戳?
EN

Stack Overflow用户
提问于 2020-04-01 21:51:18
回答 1查看 537关注 0票数 1

使用带有全局启用的时间戳的Timestamper plugin 1.11.2,使用默认格式,我得到以下控制台输出:

代码语言:javascript
复制
00:00:41.097  Some Message

在Blue Ocean中,输出如下所示:

代码语言:javascript
复制
[2020-04-01T00:00:41.097Z] Some Message

如何才能使Blue Ocean使用短时间戳格式?较长的格式有些难以阅读,并且使步骤的详细信息视图变得混乱。

我也研究了Pipeline Options,但只有timestamps选项没有指定格式的参数。

注意:This question不是一个副本,因为它只要求时区的差异。

EN

回答 1

Stack Overflow用户

发布于 2020-04-02 01:15:36

编辑:

不幸的是,这个变通方法在node的上下文中不起作用,请参阅JENKINS-59575。看起来我最终不得不亲自动手开发插件,才能以一种受支持的方式做这样的事情。

无论如何,我不会删除这个答案,因为代码在其他场景中可能仍然有用。

原始答案:

作为一种解决办法,我创建了一个自定义的ConsoleLogFilter。它可以作为管道选项、阶段选项或步骤级别应用。如果您安装了时间戳插件,则应禁用全局时间戳选项以防止重复的时间戳。

通常,您将在共享库中定义低级代码。下面是一个可以直接复制粘贴到管道脚本编辑器中的示例(您可能需要禁用Groovy沙箱):

代码语言:javascript
复制
import hudson.console.LineTransformationOutputStream
import hudson.console.ConsoleLogFilter
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets

pipeline{
    agent any
    /*
    options{
        // Enable timestamps for the whole pipeline, using default format
        //withContext( myTimestamps() )

        // Enable timestamps for the whole pipeline, using custom format
        //withContext( myTimestamps( dateFormat: 'HH:mm:ss', prefix: '', suffix: ' - ' ) )
    }
    */

    stages {
        stage('A') {
            options {
                // Enable timestamps for this stage only
                withContext( myTimestamps() )
            }
            steps {
                echo 'Hello World'
            }
        }
        stage('B') {
            steps {
                echo 'Hello World'

                // Enable timestamps for some steps only
                withMyTimestamps( dateFormat: 'HH:mm:ss') {
                    echo 'Hello World'
                }
            }
        }
    }
}

//----- Code below should be moved into a shared library -----

// For use as option at pipeline or stage level, e. g.: withContext( myTimestamps() )    
def myTimestamps( Map args = [:] ) {
    return new MyTimestampedLogFilter( args )
}

// For use as block wrapper at steps level 
void withMyTimestamps( Map args = [:], Closure block ) {
    withContext( new MyTimestampedLogFilter( args ), block )
}

class MyTimestampedLogFilter extends ConsoleLogFilter {
    String dateFormat
    String prefix
    String suffix

    MyTimestampedLogFilter( Map args = [:] ) {
        this.dateFormat = args.dateFormat ?: 'YY-MM-dd HH:mm:ss'
        this.prefix     = args.prefix ?: '['
        this.suffix     = args.suffix ?: '] '
    }

    @NonCPS
    OutputStream decorateLogger( AbstractBuild build, OutputStream logger )
            throws IOException, InterruptedException {

        return new MyTimestampedOutputStream( logger, StandardCharsets.UTF_8, this.dateFormat, this.prefix, this.suffix )
    }
}

class MyTimestampedOutputStream extends LineTransformationOutputStream {
    OutputStream logger
    Charset charset
    String dateFormat
    String prefix
    String suffix

    MyTimestampedOutputStream( OutputStream logger, Charset charset, String dateFormat, String prefix, String suffix ) {
        this.logger = logger
        this.charset = charset
        this.dateFormat = dateFormat
        this.prefix = prefix
        this.suffix = suffix
    }

    @NonCPS
    void close() throws IOException {
        super.close();
        logger.close();
    }

    @NonCPS
    void eol( byte[] bytes, int len ) throws IOException {
        def lineIn = charset.decode( java.nio.ByteBuffer.wrap( bytes, 0, len ) ).toString()
        def dateFormatted = new Date().format( this.dateFormat )
        def lineOut = "${this.prefix}${dateFormatted}${this.suffix}${lineIn}\n" 
        logger.write( lineOut.getBytes( charset ) )
    }
}

阶段“B”的输出示例:

致谢:

我是从this answer得到这个想法的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60972443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档