5

idea一键生成日志,爽!!!

 2 years ago
source link: https://segmentfault.com/a/1190000040779691
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

问题

项目开发过程中,一个方法的日志打印至少需要三种:
1、参数打印
2、返回结果打印
3、异常打印

以前的解决办法

硬着头皮慢慢敲

好处:让你多熟悉几遍代码
坏处:1、每次打印的日志格式很难统一 2、费力费心(不能早早下班)

现在的解决办法

通过idea live templates自动生成:

1、loge 异常日志打印
log.error($content$,$params$);
content 参数:
groovyScript("def params = _2.collect {'【'+it+' = {}】'}.join(', '); return '\"' + _1 + '() called with exception => ' + (params.empty ? '' : params) + '\"'", methodName(), methodParameters())
params参数:
groovyScript("def params = _1.collect {it}.join(', '); return (params.empty ? '' : params) + ',e' ", methodParameters())

image.png

2、logi 参数日志打印
log.info($content$,$params$);
content参数:
groovyScript("def params = _2.collect {'【'+it+' = {}】'}.join(', '); return '\"' + _1 + '() called with parameters => ' + (params.empty ? '' : params) + '\"'", methodName(), methodParameters())
params参数:
groovyScript("def params = _1.collect {it}.join(', '); return (params.empty ? '' : params) ", methodParameters())

3、logr 结果日志打印
log.info("$METHOD_NAME$() returned: " + $result$);
METHOD_NAME参数:
methodName()
result参数:
variableOfType(methodReturnType())

Respect!

将来的解决办法:

请大家把更好的方法留在评论区!!!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK