Skip to content

Commit 20d8fc1

Browse files
authored
Merge pull request #336 from librespot-org/lib-slf4j
Use slf4j for logging in lib module
2 parents 4681b9f + ba7c05a commit 20d8fc1

27 files changed

Lines changed: 147 additions & 117 deletions

api/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@
5656
<version>${project.version}</version>
5757
</dependency>
5858

59+
<!-- Logging -->
60+
<dependency>
61+
<groupId>org.apache.logging.log4j</groupId>
62+
<artifactId>log4j-core</artifactId>
63+
<version>${log4j-core.version}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.apache.logging.log4j</groupId>
67+
<artifactId>log4j-slf4j-impl</artifactId>
68+
<version>${log4j-slf4j-impl.version}</version>
69+
<scope>runtime</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>com.lmax</groupId>
73+
<artifactId>disruptor</artifactId>
74+
<version>${lmax-disruptor.version}</version>
75+
<scope>runtime</scope>
76+
</dependency>
77+
78+
<!-- HTTP server -->
5979
<dependency>
6080
<groupId>io.undertow</groupId>
6181
<artifactId>undertow-core</artifactId>

lib/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,12 @@
9898
<artifactId>commons-net</artifactId>
9999
<version>3.8.0</version>
100100
</dependency>
101+
102+
<!-- Logging -->
103+
<dependency>
104+
<groupId>org.slf4j</groupId>
105+
<artifactId>slf4j-api</artifactId>
106+
<version>1.7.30</version>
107+
</dependency>
101108
</dependencies>
102109
</project>

lib/src/main/java/xyz/gianlu/librespot/ZeroconfServer.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import com.google.gson.JsonObject;
44
import com.spotify.connectstate.Connect;
55
import okhttp3.HttpUrl;
6-
import org.apache.logging.log4j.LogManager;
7-
import org.apache.logging.log4j.Logger;
86
import org.jetbrains.annotations.NonNls;
97
import org.jetbrains.annotations.NotNull;
108
import org.jetbrains.annotations.Nullable;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
1111
import xyz.gianlu.librespot.common.NameThreadFactory;
1212
import xyz.gianlu.librespot.common.Utils;
1313
import xyz.gianlu.librespot.core.Session;
@@ -40,7 +40,7 @@
4040
public class ZeroconfServer implements Closeable {
4141
private final static int MAX_PORT = 65536;
4242
private final static int MIN_PORT = 1024;
43-
private static final Logger LOGGER = LogManager.getLogger(ZeroconfServer.class);
43+
private static final Logger LOGGER = LoggerFactory.getLogger(ZeroconfServer.class);
4444
private static final byte[] EOL = new byte[]{'\r', '\n'};
4545
private static final JsonObject DEFAULT_GET_INFO_FIELDS = new JsonObject();
4646
private static final JsonObject DEFAULT_SUCCESSFUL_ADD_USER = new JsonObject();
@@ -247,19 +247,19 @@ private void handleGetInfo(OutputStream out, String httpVersion) throws IOExcept
247247
private void handleAddUser(OutputStream out, Map<String, String> params, String httpVersion) throws GeneralSecurityException, IOException {
248248
String username = params.get("userName");
249249
if (username == null || username.isEmpty()) {
250-
LOGGER.fatal("Missing userName!");
250+
LOGGER.error("Missing userName!");
251251
return;
252252
}
253253

254254
String blobStr = params.get("blob");
255255
if (blobStr == null || blobStr.isEmpty()) {
256-
LOGGER.fatal("Missing blob!");
256+
LOGGER.error("Missing blob!");
257257
return;
258258
}
259259

260260
String clientKeyStr = params.get("clientKey");
261261
if (clientKeyStr == null || clientKeyStr.isEmpty()) {
262-
LOGGER.fatal("Missing clientKey!");
262+
LOGGER.error("Missing clientKey!");
263263
return;
264264
}
265265

@@ -300,7 +300,7 @@ private void handleAddUser(OutputStream out, Map<String, String> params, String
300300
byte[] mac = hmac.doFinal();
301301

302302
if (!Arrays.equals(mac, checksum)) {
303-
LOGGER.fatal("Mac and checksum don't match!");
303+
LOGGER.error("Mac and checksum don't match!");
304304

305305
out.write(httpVersion.getBytes());
306306
out.write(" 400 Bad Request".getBytes()); // I don't think this is the Spotify way
@@ -355,7 +355,7 @@ private void handleAddUser(OutputStream out, Map<String, String> params, String
355355

356356
sessionListeners.forEach(l -> l.sessionChanged(session));
357357
} catch (Session.SpotifyAuthenticationException | MercuryClient.MercuryException | IOException | GeneralSecurityException ex) {
358-
LOGGER.fatal("Couldn't establish a new session.", ex);
358+
LOGGER.error("Couldn't establish a new session.", ex);
359359

360360
synchronized (connectionLock) {
361361
connectingUsername = null;
@@ -465,11 +465,11 @@ public void run() {
465465
handle(socket);
466466
socket.close();
467467
} catch (IOException ex) {
468-
LOGGER.fatal("Failed handling request!", ex);
468+
LOGGER.error("Failed handling request!", ex);
469469
}
470470
});
471471
} catch (IOException ex) {
472-
if (!shouldStop) LOGGER.fatal("Failed handling connection!", ex);
472+
if (!shouldStop) LOGGER.error("Failed handling connection!", ex);
473473
}
474474
}
475475
}
@@ -481,13 +481,13 @@ private void handleRequest(@NotNull OutputStream out, @NotNull String httpVersio
481481
try {
482482
handleAddUser(out, params, httpVersion);
483483
} catch (GeneralSecurityException | IOException ex) {
484-
LOGGER.fatal("Failed handling addUser!", ex);
484+
LOGGER.error("Failed handling addUser!", ex);
485485
}
486486
} else if (Objects.equals(action, "getInfo")) {
487487
try {
488488
handleGetInfo(out, httpVersion);
489489
} catch (IOException ex) {
490-
LOGGER.fatal("Failed handling getInfo!", ex);
490+
LOGGER.error("Failed handling getInfo!", ex);
491491
}
492492
} else {
493493
LOGGER.warn("Unknown action: " + action);
@@ -522,13 +522,13 @@ private void handle(@NotNull Socket socket) throws IOException {
522522
if (Objects.equals(method, "POST")) {
523523
String contentType = headers.get("Content-Type");
524524
if (!Objects.equals(contentType, "application/x-www-form-urlencoded")) {
525-
LOGGER.fatal("Bad Content-Type: " + contentType);
525+
LOGGER.error("Bad Content-Type: " + contentType);
526526
return;
527527
}
528528

529529
String contentLengthStr = headers.get("Content-Length");
530530
if (contentLengthStr == null) {
531-
LOGGER.fatal("Missing Content-Length header!");
531+
LOGGER.error("Missing Content-Length header!");
532532
return;
533533
}
534534

lib/src/main/java/xyz/gianlu/librespot/audio/AudioKeyManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package xyz.gianlu.librespot.audio;
22

33
import com.google.protobuf.ByteString;
4-
import org.apache.logging.log4j.LogManager;
5-
import org.apache.logging.log4j.Logger;
64
import org.jetbrains.annotations.NotNull;
75
import org.jetbrains.annotations.Nullable;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
88
import xyz.gianlu.librespot.common.Utils;
99
import xyz.gianlu.librespot.core.PacketsReceiver;
1010
import xyz.gianlu.librespot.core.Session;
@@ -24,7 +24,7 @@
2424
*/
2525
public final class AudioKeyManager implements PacketsReceiver {
2626
private static final byte[] ZERO_SHORT = new byte[]{0, 0};
27-
private static final Logger LOGGER = LogManager.getLogger(AudioKeyManager.class);
27+
private static final Logger LOGGER = LoggerFactory.getLogger(AudioKeyManager.class);
2828
private static final long AUDIO_KEY_REQUEST_TIMEOUT = 2000;
2929
private final AtomicInteger seqHolder = new AtomicInteger(0);
3030
private final Map<Integer, Callback> callbacks = Collections.synchronizedMap(new HashMap<>());
@@ -109,7 +109,7 @@ public void key(byte[] key) {
109109

110110
@Override
111111
public void error(short code) {
112-
LOGGER.fatal("Audio key error, code: {}", code);
112+
LOGGER.error("Audio key error, code: {}", code);
113113

114114
synchronized (reference) {
115115
reference.set(null);

lib/src/main/java/xyz/gianlu/librespot/audio/NormalizationData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package xyz.gianlu.librespot.audio;
22

33

4-
import org.apache.logging.log4j.LogManager;
5-
import org.apache.logging.log4j.Logger;
64
import org.jetbrains.annotations.NotNull;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
77

88
import java.io.DataInputStream;
99
import java.io.IOException;
@@ -15,7 +15,7 @@
1515
* @author Gianlu
1616
*/
1717
public class NormalizationData {
18-
private static final Logger LOGGER = LogManager.getLogger(NormalizationData.class);
18+
private static final Logger LOGGER = LoggerFactory.getLogger(NormalizationData.class);
1919
public final float track_gain_db;
2020
public final float track_peak;
2121
public final float album_gain_db;

lib/src/main/java/xyz/gianlu/librespot/audio/PlayableContentFeeder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import okhttp3.HttpUrl;
77
import okhttp3.Response;
88
import okhttp3.ResponseBody;
9-
import org.apache.logging.log4j.LogManager;
10-
import org.apache.logging.log4j.Logger;
119
import org.jetbrains.annotations.Contract;
1210
import org.jetbrains.annotations.NotNull;
1311
import org.jetbrains.annotations.Nullable;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
1414
import xyz.gianlu.librespot.audio.cdn.CdnFeedHelper;
1515
import xyz.gianlu.librespot.audio.cdn.CdnManager;
1616
import xyz.gianlu.librespot.audio.format.AudioQualityPicker;
@@ -30,7 +30,7 @@
3030
* @author Gianlu
3131
*/
3232
public final class PlayableContentFeeder {
33-
private static final Logger LOGGER = LogManager.getLogger(PlayableContentFeeder.class);
33+
private static final Logger LOGGER = LoggerFactory.getLogger(PlayableContentFeeder.class);
3434
private static final String STORAGE_RESOLVE_INTERACTIVE = "/storage-resolve/files/audio/interactive/%s";
3535
private static final String STORAGE_RESOLVE_INTERACTIVE_PREFETCH = "/storage-resolve/files/audio/interactive_prefetch/%s";
3636
protected final Session session;
@@ -84,7 +84,7 @@ private StorageResolveResponse resolveStorageInteractive(@NotNull ByteString fil
8484
String country = session.countryCode();
8585
if (country != null) ContentRestrictedException.checkRestrictions(country, original.getRestrictionList());
8686

87-
LOGGER.fatal("Couldn't find playable track: " + id.toSpotifyUri());
87+
LOGGER.error("Couldn't find playable track: " + id.toSpotifyUri());
8888
throw new FeederException();
8989
}
9090

@@ -134,7 +134,7 @@ private LoadedStream loadStream(@NotNull Metadata.AudioFile file, @Nullable Meta
134134
private LoadedStream loadTrack(@NotNull Metadata.Track track, @NotNull AudioQualityPicker audioQualityPicker, boolean preload, @Nullable HaltListener haltListener) throws IOException, CdnManager.CdnException, MercuryClient.MercuryException {
135135
Metadata.AudioFile file = audioQualityPicker.getFile(track.getFileList());
136136
if (file == null) {
137-
LOGGER.fatal("Couldn't find any suitable audio file, available: {}", Utils.formatsToString(track.getFileList()));
137+
LOGGER.error("Couldn't find any suitable audio file, available: {}", Utils.formatsToString(track.getFileList()));
138138
throw new FeederException();
139139
}
140140

@@ -150,7 +150,7 @@ private LoadedStream loadEpisode(@NotNull EpisodeId id, @NotNull AudioQualityPic
150150
} else {
151151
Metadata.AudioFile file = audioQualityPicker.getFile(episode.getAudioList());
152152
if (file == null) {
153-
LOGGER.fatal("Couldn't find any suitable audio file, available: {}", Utils.formatsToString(episode.getAudioList()));
153+
LOGGER.error("Couldn't find any suitable audio file, available: {}", Utils.formatsToString(episode.getAudioList()));
154154
throw new FeederException();
155155
}
156156

lib/src/main/java/xyz/gianlu/librespot/audio/cdn/CdnFeedHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import okhttp3.HttpUrl;
66
import okhttp3.Request;
77
import okhttp3.Response;
8-
import org.apache.logging.log4j.LogManager;
9-
import org.apache.logging.log4j.Logger;
108
import org.jetbrains.annotations.NotNull;
119
import org.jetbrains.annotations.Nullable;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
1212
import xyz.gianlu.librespot.audio.HaltListener;
1313
import xyz.gianlu.librespot.audio.NormalizationData;
1414
import xyz.gianlu.librespot.audio.PlayableContentFeeder;
@@ -23,7 +23,7 @@
2323
* @author Gianlu
2424
*/
2525
public final class CdnFeedHelper {
26-
private static final Logger LOGGER = LogManager.getLogger(CdnFeedHelper.class);
26+
private static final Logger LOGGER = LoggerFactory.getLogger(CdnFeedHelper.class);
2727

2828
private CdnFeedHelper() {
2929
}

lib/src/main/java/xyz/gianlu/librespot/audio/cdn/CdnManager.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import com.spotify.metadata.Metadata;
55
import com.spotify.storage.StorageResolve.StorageResolveResponse;
66
import okhttp3.*;
7-
import org.apache.logging.log4j.LogManager;
8-
import org.apache.logging.log4j.Logger;
97
import org.jetbrains.annotations.NotNull;
108
import org.jetbrains.annotations.Nullable;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
1111
import xyz.gianlu.librespot.audio.*;
1212
import xyz.gianlu.librespot.audio.decrypt.AesAudioDecrypt;
1313
import xyz.gianlu.librespot.audio.decrypt.AudioDecrypt;
@@ -33,7 +33,7 @@
3333
* @author Gianlu
3434
*/
3535
public class CdnManager {
36-
private static final Logger LOGGER = LogManager.getLogger(CdnManager.class);
36+
private static final Logger LOGGER = LoggerFactory.getLogger(CdnManager.class);
3737
private final Session session;
3838

3939
public CdnManager(@NotNull Session session) {
@@ -294,15 +294,15 @@ private void requestChunk(int index) {
294294
return;
295295
}
296296
} catch (IOException | CacheManager.BadChunkHashException ex) {
297-
LOGGER.fatal("Failed requesting chunk from cache, index: {}", index, ex);
297+
LOGGER.error("Failed requesting chunk from cache, index: {}", index, ex);
298298
}
299299
}
300300

301301
try {
302302
InternalResponse resp = request(index);
303303
writeChunk(resp.buffer, index, false);
304304
} catch (IOException | CdnException ex) {
305-
LOGGER.fatal("Failed requesting chunk from network, index: {}", index, ex);
305+
LOGGER.error("Failed requesting chunk from network, index: {}", index, ex);
306306
internalStream.notifyChunkError(index, new AbsChunkedInputStream.ChunkException(ex));
307307
}
308308
}

lib/src/main/java/xyz/gianlu/librespot/audio/storage/AudioFileFetch.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package xyz.gianlu.librespot.audio.storage;
22

3-
import org.apache.logging.log4j.LogManager;
4-
import org.apache.logging.log4j.Logger;
53
import org.jetbrains.annotations.NotNull;
64
import org.jetbrains.annotations.Nullable;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
77
import xyz.gianlu.librespot.audio.AbsChunkedInputStream;
88
import xyz.gianlu.librespot.cache.CacheManager;
99
import xyz.gianlu.librespot.common.Utils;
@@ -19,7 +19,7 @@
1919
public class AudioFileFetch implements AudioFile {
2020
public static final byte HEADER_SIZE = 0x3;
2121
public static final byte HEADER_CDN = 0x4;
22-
private static final Logger LOGGER = LogManager.getLogger(AudioFileFetch.class);
22+
private static final Logger LOGGER = LoggerFactory.getLogger(AudioFileFetch.class);
2323
private final CacheManager.Handler cache;
2424
private int size = -1;
2525
private int chunks = -1;
@@ -65,7 +65,7 @@ public synchronized void writeHeader(int id, byte[] bytes, boolean cached) throw
6565

6666
@Override
6767
public synchronized void streamError(int chunkIndex, short code) {
68-
LOGGER.fatal("Stream error, index: {}, code: {}", chunkIndex, code);
68+
LOGGER.error("Stream error, index: {}, code: {}", chunkIndex, code);
6969

7070
exception = AbsChunkedInputStream.ChunkException.fromStreamError(code);
7171
notifyAll();

lib/src/main/java/xyz/gianlu/librespot/audio/storage/AudioFileStreaming.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import com.google.protobuf.ByteString;
44
import com.spotify.metadata.Metadata;
5-
import org.apache.logging.log4j.LogManager;
6-
import org.apache.logging.log4j.Logger;
75
import org.jetbrains.annotations.NotNull;
86
import org.jetbrains.annotations.Nullable;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
99
import xyz.gianlu.librespot.audio.AbsChunkedInputStream;
1010
import xyz.gianlu.librespot.audio.GeneralAudioStream;
1111
import xyz.gianlu.librespot.audio.HaltListener;
@@ -28,7 +28,7 @@
2828
* @author Gianlu
2929
*/
3030
public class AudioFileStreaming implements AudioFile, GeneralAudioStream {
31-
private static final Logger LOGGER = LogManager.getLogger(AudioFileStreaming.class);
31+
private static final Logger LOGGER = LoggerFactory.getLogger(AudioFileStreaming.class);
3232
private final CacheManager.Handler cacheHandler;
3333
private final Metadata.AudioFile file;
3434
private final byte[] key;
@@ -72,7 +72,7 @@ private void requestChunk(@NotNull ByteString fileId, int index, @NotNull AudioF
7272
try {
7373
session.channel().requestChunk(fileId, index, file);
7474
} catch (IOException ex) {
75-
LOGGER.fatal("Failed requesting chunk from network, index: {}", index, ex);
75+
LOGGER.error("Failed requesting chunk from network, index: {}", index, ex);
7676
chunksBuffer.internalStream.notifyChunkError(index, new AbsChunkedInputStream.ChunkException(ex));
7777
}
7878
}
@@ -84,7 +84,7 @@ private boolean tryCacheChunk(int index) {
8484
cacheHandler.readChunk(index, this);
8585
return true;
8686
} catch (IOException | CacheManager.BadChunkHashException ex) {
87-
LOGGER.fatal("Failed requesting chunk from cache, index: {}", index, ex);
87+
LOGGER.error("Failed requesting chunk from cache, index: {}", index, ex);
8888
return false;
8989
}
9090
}
@@ -147,7 +147,7 @@ public void writeHeader(int id, byte[] bytes, boolean cached) {
147147

148148
@Override
149149
public void streamError(int chunkIndex, short code) {
150-
LOGGER.fatal("Stream error, index: {}, code: {}", chunkIndex, code);
150+
LOGGER.error("Stream error, index: {}, code: {}", chunkIndex, code);
151151
chunksBuffer.internalStream.notifyChunkError(chunkIndex, AbsChunkedInputStream.ChunkException.fromStreamError(code));
152152
}
153153

0 commit comments

Comments
 (0)