小编mt_*_*mt_的帖子

Java:如何将原始JSON记录为JSON,并避免在使用logback/slf4j进行日志记录期间进行转义

我在JAX-RS应用程序中使用带有Logback的SLF4J ...我想以这样的方式登录到JSON,即我的消息不再被编码,而是原始打印到日志文件中:

目前它看起来像这样:

{"@timestamp":1363834123012,"@message":"{\"text\":\"From MLK to Barack 
Ob...\n\"}"
Run Code Online (Sandbox Code Playgroud)

但我希望有这个:

  {"@timestamp":1363834123012,"@message": { "text ": "From MLK to Barack 
Ob...\n\}
Run Code Online (Sandbox Code Playgroud)

原因是我想再次解析JSON并希望避免数据的转义.

我写了一个自定义的logback编码器,但我发现无法避免转义.我可以传递一个对象进行回溯并根据对象的类型更改设置吗?

编辑:我找到了一种方式 - 不完全优雅 - 按照SSCE的要求:

在我的申请中

// SLF4J Logger
private static Logger logger = LoggerFactory.getLogger(MyClass.class);
// A logback? Marker
private Marker foo = MarkerFactory.getMarker("foo");
// Jackson ObjectMapper()
ObjectMapper mapper = new ObjectMapper();

// Log something... 
logger.info(foo, mapper.writeValueAsString(json));
Run Code Online (Sandbox Code Playgroud)

我在这里使用了Logstash-Encoder的变体:https://github.com/logstash/logstash-logback-encoder

package my.package;

import static org.apache.commons.io.IOUtils.*;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import org.codehaus.jackson.JsonGenerator.Feature;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ObjectNode; …
Run Code Online (Sandbox Code Playgroud)

java logging json logback slf4j

14
推荐指数
2
解决办法
1万
查看次数

locales如何在Linux/POSIX中工作以及应用了哪些转换?

我正在处理(希望)UTF-8文本的大文件.我可以使用Ubuntu 13.10(3.11.0-14-generic)和12.04重现它.

在调查一个bug我遇到了奇怪的行为

$ export LC_ALL=en_US.UTF-8   
$ sort part-r-00000 | uniq -d 
? ? ? ? 251
? ? ? ? ?       291
? ? ? ? 301
? ?     475
? ?     565

$ export LC_ALL=C
$ sort part-r-00000 | uniq -d 
$ # no duplicates found
Run Code Online (Sandbox Code Playgroud)

运行使用读取文件的自定义C++程序时也会出现重复项std::stringstream- 由于使用en_US.UTF-8区域设置时出现重复,它会失败. C++似乎至少对于std::string输入/输出没有影响.

为什么在使用UTF-8语言环境时找到重复项,并且在C语言环境中找不到重复项?

导致此行为的文本的区域设置有哪些转换?

编辑:是一个小例子

$ uniq -D duplicates.small.nfc 
? ? ? ? ?       224
? ? ? ? ?       224
? ? …
Run Code Online (Sandbox Code Playgroud)

linux locale posix utf-8 gnu-coreutils

11
推荐指数
1
解决办法
5525
查看次数

std :: packaged_task编译错误w/gcc 4.6

我正在尝试使用一个线程启动一个函数 std::packaged_task

Query query; /* protobuf object */        

/* fill Query object */

std::packaged_task<SearchResults(Query&)> task([](Query& q) ->SearchResults {
    index::core::Merger merger;
    return merger.search(q);
    });

std::future<SearchResults> ftr = task.get_future();
std::thread(std::move(task),query).detach();
Run Code Online (Sandbox Code Playgroud)

Edit2:再次更新代码以修复错误并包含完整的错误消息.

g ++ - 4.6(在Ubuntu 10.04上)无法编译代码:

In file included from /usr/include/c++/4.6/memory:80:0,
              from ../src/net/QueryConnection.cpp:8:
/usr/include/c++/4.6/functional: In instantiation of ‘std::_Bind_result<void, 
std::packaged_task<SearchResults(Query&)>(Query)>’:
/usr/include/c++/4.6/thread:135:9:   instantiated from ‘std::thread::thread(_Callable&&, 
_Args&& ...) [with _Callable = std::packaged_task<SearchResults(Query&)>, _Args = 
{Query&}]’
../src/net/QueryConnection.cpp:77:36:   instantiated from here
/usr/include/c++/4.6/functional:1365:7: error: ‘std::_Bind_result<_Result, 
_Functor(_Bound_args ...)>::_Bind_result(const std::_Bind_result<_Result, 
_Functor(_Bound_args ...)>&) [with _Result = void, _Functor =   
std::packaged_task<SearchResults(Query&)>, _Bound_args …
Run Code Online (Sandbox Code Playgroud)

c++ concurrency g++ std c++11

5
推荐指数
1
解决办法
777
查看次数

命名空间中的全局变量 - 值在线程中不同

考虑以下szenario:

  • boost::asio每个线程中有2个不同的网络端口
  • 1个端口正在接收和处理数据 - class DataConnection包装在一个std::thread
  • 1端口用于发送统计信息class StatConnection也包含在std::thread

为了计算连接(和其他小数据)我的想法是staticnamespace类似的内部使用变量:

#include <atomic>

namespace app {
 namespace status {
   static std::atomic<long> counter = 0; 
 }
}
Run Code Online (Sandbox Code Playgroud)

这适用于DataConnection班级.在这里,我增加counterc'tor并查看值增量.

counter在我的StatConnection班上总是如此0

为什么会这样?

我尝试了一些替代品:

  • 交换std::atomic<long>static volatile long:没有了作用.
  • 使用没有static关键字的命名空间

然后我收到链接器错误:

multiple definition of `app::status::searchtime'
./src/status/Status.o:/[...]/include/status/Status.hpp:16: first defined here
[...]
Run Code Online (Sandbox Code Playgroud)

那么为什么count线程之间的价值不同呢?

c++ multithreading atomic c++11

5
推荐指数
1
解决办法
5733
查看次数

奇怪的 Java 泛型编译错误:参数化方法不适用于相同的参数化类型?

我偶然发现了这个错误,但找不到问题所在:

The method compute(Set<String>, Set<String>) 
in the type JaccardSimilarity<String> is not applicable 
for the arguments (Set<String>, Set<String>)
Run Code Online (Sandbox Code Playgroud)

有问题的方法是使用泛型:

public class JaccardSimilarity<E> implements SimilarityMeasure<Set<E>, Set<E>> {

    @Override
    public double compute(Set<E> s1, Set<E> s2){
        // compute some stuff
    return unionSize == 0 ? 1 : intersectionSize / (double)unionSize;
    }
}
Run Code Online (Sandbox Code Playgroud)

我在课堂上这样称呼它:

public MyClass {
    private JaccardSimilarity<String> sim = new JaccardSimilarity<String>();

    public void calc() {
    Set<String> s1 = new HashSet<>();
    s1.add("hallo welt");
    Set<String> s2 = new HashSet<>();
    s2.add("hallo welt");
            // the line below …
Run Code Online (Sandbox Code Playgroud)

java generics

1
推荐指数
1
解决办法
613
查看次数

while循环和substring()导致StringIndexOutOfBoundsException

我试图仅从String的开头替换非alpanumeric字符.我想出了以下代码:

public static final class FixNonAlphaNumBegin implements PhraseFilter {
    @Override
    public String filter(String phrase, long frequency) {
        int count = 0;

    while (!Character.isLetterOrDigit(phrase.codePointAt(count))
                && phrase.length() > count + 1) {
            phrase = phrase.replaceFirst(
                    Pattern.quote(phrase.substring(count, count + 1)), "");
            count++;
        }


        return phrase.trim();           
    }
}
Run Code Online (Sandbox Code Playgroud)

IndexOutOfBoundsException来自哪里?它应该是不可能的:

例如

String filter = "! " 

!Character.isLettorDigit(phrase.codePointAt(0) --> true 
phrase.length() > 1 --> true

phrase = phrase.replaceFirst(
                Pattern.quote(phrase.substring(0, 0 + 1)), "");
Run Code Online (Sandbox Code Playgroud)

phrase现在" ",count1

!Character.isLettorDigit(phrase.codePointAt(1) --> true 
phrase.length() > 2 …
Run Code Online (Sandbox Code Playgroud)

java string while-loop

0
推荐指数
1
解决办法
209
查看次数